How to get private member value in C #

I want to get the value of a private member, so I wrote the following:

var f = e.
          GetType().
          GetFields(System.Reflection.BindingFlags.NonPublic |
                    System.Reflection.BindingFlags.Instance | 
                    System.Reflection.BindingFlags.DeclaredOnly)[0];
object o = f.FieldType.GetProperty("RowIndex").GetValue(f.FieldType, null);

but the GetValue method needs the source object in the first parameter, and I don't have this object, because I get it at runtime. Can anyone help me ?!

+3
source share
2 answers

I think in your example the original object would ebe right?

Kindness,

Dan

+11
source

You might be lucky if you truly split this reflected call into its components. It seems to me that the original object will actually be the PropertyInfo generated by

f.FieldType.GetProperty("RowIndex")

, PropertyInfo , GetValue, , .

0

Source: https://habr.com/ru/post/1728939/


All Articles