简体   繁体   English

反射获取FieldInfo对象的类型?

[英]Reflection get type of FieldInfo object?

HI All, I need to access the class SomeClass which is declared has a private field in the Wrapper class, using Reflection so far i have been able to get private field members . 全部,我需要访问声明的类SomeClass在Wrapper类中有一个私有字段,使用Reflection到目前为止我已经能够获得私有字段成员。 How do i cast it back to its original type so that i could access it properties and other members. 如何将其转换回原始类型,以便我可以访问它的属性和其他成员。

internal class Program
    {
        private static void Main(string[] args)
        {
            Wrapper wrap = new Wrapper
            {
                SOmeProperty = new SomeClass
                {
                    Number = 007
                }
            };

            Type type = wrap.GetType();
            FieldInfo[] infos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (var item in infos)
            {
            }
        }
    }

    internal class SomeClass
    {
        public int Number { get; set; }
    }

    internal class Wrapper
    {
        private SomeClass _tempSomeObj;

        public SomeClass SOmeProperty
        {
            get
            {
                return _tempSomeObj;
            }
            set
            {
                _tempSomeObj = value;
            }
        }
    }

I dont know if i understand the question correct. 我不知道我是否理解这个问题是正确的。 You want the type of the private field (backing field)?? 你想要私人领域的类型(支持领域)?

Then you could check the FieldType property of the FieldInfo.... 然后你可以检查FieldInfo的FieldType属性....

like this: 像这样:

internal class Program
{
    #region Methods

    private static void Main(string[] args)
    {
        var wrap = new Wrapper { SOmeProperty = new SomeClass { Number = 007 } };
        Type type = wrap.GetType();

        FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (var fieldInfo in fieldInfos)
        {
            if (fieldInfo.FieldType == typeof(SomeClass))
            {
                Console.WriteLine("Yap!");
            }
        }
    }

    #endregion
}

internal class SomeClass
{
    #region Properties

    public int Number { get; set; }

    #endregion
}

internal class Wrapper
{
    #region Properties

    public SomeClass SOmeProperty { get; set; }

    #endregion
}

Use PropertyInfo instead: 请改用PropertyInfo

internal class Program
{
    private static void Main(string[] args)
    {
        Wrapper wrap = new Wrapper
        {
            SOmeProperty = new SomeClass
            {
                Number = 007
            }
        };

        Type type = wrap.GetType();
        PropertyInfo info = type.GetProperty("SOmeProperty", BindingFlags.NonPublic | BindingFlags.Instance);
        SomeClass value = (SomeClass)info.GetValue(wrap, null);
        // use `value` variable here
    }
}

I'm still a little fuzzy about what your're trying to do, but you can always GetType() on any object and get its actual run time type and query that for properties field of some other type for example 关于你想要做什么,我仍然有点模糊,但你总是可以在任何对象上获取GetType()并得到它的实际运行时类型,并查询其他类型的属性字段,例如

public void ListPropertiesOfType( object targetObject, Type propertyType ) {
  foreach( var foundProperty in targetObject.GetType( ).GetProperties( ).Where( p => p.PropertyType == propertyType ) ) {
    Console.WriteLine( "Name: {0}, Value: {1}", foundProperty.Name, foundProperty.GetValue( targetObject, null ) );
  }
}

ListPropertiesOfType(new Wrapper(), typeof(SomeClass))
ListPropertiesOfType(new Wrapper(), typeof(SomeOtherClass))

If you want to pass in instances of Someclass and SomeClass that is also fine, just use GetType() on the instances to get the type that you can then use to find properties of that type as illustrated above. 如果你想传入Someclass和SomeClass的实例也很好,只需在实例上使用GetType()来获取你可以用来查找该类型属性的类型,如上所示。 this works the same way regardless if you make the method generic and pass in "T" or if its non-generic and you pass in "object" 无论你是如何使方法通用并传入“T”,或者如果它是非泛型的并传入“对象”,这都是一样的

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

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