简体   繁体   English

从 FieldInfo 获取对象实例

[英]Get an object instance from FieldInfo

So my problem is this: I have an object instance represented only by FieldInfo.所以我的问题是:我有一个仅由 FieldInfo 表示的对象实例。 I have no other access to that object.我没有其他访问该对象的权限。 Is it possible to get that object instance only through the field info like so?是否可以像这样仅通过字段信息获取该对象实例?

MyObject myObject = fieldInfo.SomeMethodOrProperty as MyObject; 

This is just only an illustration, but I think you get the picture.这只是一个插图,但我想你已经明白了。

No, the FieldInfo represents attributes and metadata of a field in regards to a type.不, FieldInfo表示与类型有关的FieldInfo属性和元数据。 It is not attached to a specific instance.它不附加到特定实例。 When you want to use it, you have to pass in the instance you want it to use (through GetValue or SetValue )当你想使用它时,你必须传入你想要它使用的实例(通过GetValueSetValue

Despite the other answer saying no, this is possible.尽管其他答案说不,但这是可能的。
I had the same problem and after some test I found this code to work:我遇到了同样的问题,经过一些测试,我发现这段代码可以工作:

Type oType = typeof(MyClass);
FieldInfo[] aoFieldInfo = oType.GetFields (BindingFlags.Public | BindingFlags.Static);

foreach (FieldInfo oFieldInfo in aoFieldInfo)
{
  object oValue = oFieldInfo.GetValue (null);
}

and it returns the values / objects of all members, of which I previously queried the FieldInfo .它返回所有成员的值/对象,我之前查询过这些成员的FieldInfo

I know this old but yes it is possible as long as you can have access to the instance of the parent object of the object in question...我知道这是旧的,但是是的,只要您可以访问相关对象的父对象的实例,就可以......

for example:例如:

public AxisObject getAxisObjectByName(string name)
    {
        FieldInfo[] Comboproperties = typeof(DrivesObject).GetFields();
        foreach (var property in Comboproperties)
        {
            if (property.Name == name)
            {
                return (AxisObject)property.GetValue(PersistantVariables.Instance.Drives);//pass the instance you're wanting to return child object instance from
            }
        }
        return null;
    }

I too was searching for this answer, found some inspiration in Tobias Knauss's answer.我也在寻找这个答案,在 Tobias Knauss 的答案中找到了一些灵​​感。

Hopefully this helps someone as well.希望这也有助于某人。

With my fields placed in this Holiday class, I wanted to access them all in a collection.将我的字段放在这个 Holiday 类中后,我想在一个集合中访问它们。

    class Holiday
    {
        public static readonly DateTime New_Years_Day = new DateTime(year: DateTime.Now.AddYears(1).Year, month: 1, day: 1);
        public static readonly DateTime Christmas_Day = new DateTime(year: DateTime.Now.Year, month: 12, day: 24);
        public static readonly DateTime Thanksgiving_Day = new DateTime(year: DateTime.Now.Year, month: 11, day: 25);
        public static readonly DateTime Labor_Day = new DateTime(year: DateTime.Now.Year, month: 9, day: 6);
        public static readonly DateTime Memorial_Day = new DateTime(year: DateTime.Now.Year, month: 5, day: 31);
                                                                      

}

To test I created a method to run my test:为了测试,我创建了一个方法来运行我的测试:

    static DateTime[] get_all_holidays()
    {  
        DateTime[] _holidays = 
            typeof(Holiday)
            .GetFields()
            .Select(s => 
                (DateTime)s.GetValue(null))
                .ToArray(); 

        foreach (DateTime holiday in _holidays)
        {
            Debug.WriteLine(holiday.ToString());

        }

        return _holidays;
    }

When I run the above code, I check my locals and see all the dates.当我运行上面的代码时,我检查我的当地人并查看所有日期。 在此处输入图片说明

And finally implemented into a property for quick access:最后实现到一个属性中以便快速访问:

    public static DateTime[] All_Holidays {
        get {
            return 
                typeof(Holiday)
                    .GetFields()
                    .Select(s =>
                        (DateTime)s.GetValue(null))
                        .ToArray();
        } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM