简体   繁体   中英

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 . 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....

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:

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

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. 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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