C#中如何获取私有成员值
我想获取私有成员的值,所以我写了以下内容:
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);
但是方法“GetValue”需要第一个参数中的原始对象,而我没有这个对象,因为我在运行时获取。 有人可以帮助我吗?
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.BindingFlags.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?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在您的示例中,原始对象将是
e
不是吗?I think in your example, the original object would be
e
would it not?如果您实际上将此反射调用分离为其组成部分,您可能会运气更好。在我看来,原始对象实际上是由生成的 PropertyInfo
我想说的是,如果您实际上声明了一个 PropertyInfo 对象来暂时保存它,您就可以将其传递到 GetValue 调用中,然后在以下情况下销毁它:你完成了。
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
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.