开发者

How to get the private member value in C#

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

var f = e.
          GetType().
          GetFields(System.Reflection.BindingFlags.NonPublic |
                    System.Reflection.BindingFla开发者_如何学编程gs.Instance | 
                    System.Reflection.BindingFlags.DeclaredOnly)[0];
object o = f.FieldType.GetProperty("RowIndex").GetValue(f.FieldType, null);

but the method "GetValue" needs the original object in the first parameter, and I don't have this object, because I get in runtime. Could anyone help-me?!


I think in your example, the original object would be e would it not?


You might have better luck if you actually separate out this reflected call into its component pieces. It looks to me like the original object is actually going to be the PropertyInfo generated by

f.FieldType.GetProperty("RowIndex")

I'd say if you actually declare a PropertyInfo object to hold this temporarily, you'd be able to pass it into the GetValue call and then destroy it when you're done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜