Here is a code snippet on how to set private properties using Reflection.
Code Snippet
- private static void SetPrivateProperty<T>(Object obj, string propertyName, object value)
- {
- var propertyInfo = typeof(T).GetProperty(propertyName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
- if (propertyInfo == null) return;
- propertyInfo.SetValue(obj, value, null);
- }
- private static object GetPrivateProperty<T>(Object obj, string propertyName)
- {
- if (obj == null) return null;
- var propertyInfo = typeof(T).GetProperty(propertyName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
- return propertyInfo == null ? null : propertyInfo.GetValue(obj, null);
- }
End of Code Snippet
4 comments:
Great Post,really it was very helpful for us.
Thanks a lot for sharing!
I found this blog to be very useful!!
JAVA training in Bangalore
Thank you for sharing
This is a fantastic read! Your points are well-researched and clearly explained. I appreciate the time and effort you put into creating such informative content. Keep up the great work!
Nice content and well presented ✅
If you want to start a career in testing, Immek offers high-quality Software Testing Training in Chennai with job support and practical learning
loadrunner-training-in-chennai
big-data-training-in-chennai
jmeter-performance-testing-training-in-chennai
etl-training-in-chennai
selenium-training-in-chennai
manual-testing-training-in-chennai
Post a Comment