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
1 comment:
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
Post a Comment