问题描述
怎样才能获得编程引用其中一个字段信息对象是现场的对象?
例如,我想是这样的:
myFieldInfo.GetOwner(); //返回对象,其中myFieldObject是场
不幸的是,你不能因为关系工作方式恰好相反。一个字段信息对象再presents元数据独立于任何实例。有1个字段信息为对象的字段的每个实例。
这是在一般的真有关所有元数据的对象,例如类型,字段信息,MethodInfo的,等...它可以使用所述元数据对象来操作的对象的实例。例如字段信息可以用来抓住经由GetValue方法的一个实例的值。
字段信息网络= GetFieldInfo();
对象o = GetTheObject();
对象值= fi.GetValue(O);
但是元数据对象永远不会与所述类型的实例相关联。
How does one obtain programmatically a reference to the object of which a FieldInfo object is a field?
For example, I'd like something like this:
myFieldInfo.GetOwner(); // returns the object of which myFieldObject is a field
Unfortunately you can't because the relationship works the opposite way. A FieldInfo object represents metadata that is independent of any instance. There is 1 FieldInfo for every instance of an object's field.
This is true in general about all Metadata objects such as Type, FieldInfo, MethodInfo, etc ... It is possible to use the metadata objects to manipulate an instance of an object. For instance FieldInfo can be used to grab an instance value via the GetValue method.
FieldInfo fi = GetFieldInfo();
object o = GetTheObject();
object value = fi.GetValue(o);
But a metadata object won't ever be associated with an instance of the type.
这篇关于.NET字段信息 - 获取其中的对象是*是*场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!