Why reflection does not find property

醉酒当歌 提交于 2019-12-12 15:04:53

问题


I have the class:

    class Person 
    {
        public string Name { get { return "Antonio"; } }
    }

and the Code:

        IEnumerable<object> uncknownObject;

        uncknownObject = new ObservableCollection<Person>( );

        var observCol = uncknownObject.GetType( );

        var x = ( ( dynamic )observCol ).GenericTypeArguments[ 0 ];

        var y = observCol.GetProperty( "GenericTypeArguments" );

        var instance = ( Person )Activator.CreateInstance( x );

        Console.WriteLine( instance.Name ); // Print Antonio!!!

why does y == null ?

Note the picture:

the debugger shows that the property GenericTypeArguments should exist and the code shows the opossite. It can be proven that the debugger is right and that property exist because then how come x is not null. If that property exists then why y is equal to null!!!???


Edit

Thanks to Ani I now have:

        IEnumerable<object> uncknownObject;

        uncknownObject = new ObservableCollection<Person>();

        var observCol = uncknownObject.GetType();

        var genTypeArgsProperty = typeof(Type).GetProperty("UnderlyingSystemType");

        var genTypeArgsValue = (genTypeArgsProperty.GetValue(observCol, null));

        var f = genTypeArgsValue.GetType().GetMethod("GetGenericArguments");

        IEnumerable<object> result = (IEnumerable<object>)f.Invoke(genTypeArgsValue, null);

        var x = result.FirstOrDefault();

        var instance = Activator.CreateInstance(  (Type)x );

In case of curios why I needed that functionality click here


回答1:


I don't really understand what you're trying to accomplish with all this meta-meta-reflection, but you seem to have misunderstood what Type.GetProperty does. It gets meta-data for a property on the actual type represented by the System.Type instance (in this case, ObservableCollection<Person>). It does not get meta-data for a property declared on System.Type itself, unless of course you call it on a System.Type representing System.Type itself.

In your case, y is null since ObservableCollection<Person> does not have a property named "GenericTypeArguments".

Try this instead:

var genTypeArgsProperty = typeof(Type).GetProperty("GenericTypeArguments");

var genTypeArgsValue = (Type[]) (genTypeArgsProperty.GetValue(observCol, null));

var onlyTypeArgValue = genTypeArgsValue.Single();



回答2:


This code works with net framework 4:

        IEnumerable<object> uncknownObject;

        uncknownObject = new ObservableCollection<Person>();



        var observCol = uncknownObject.GetType();

        var x = ((dynamic) observCol).UnderlyingSystemType.GetGenericArguments()[0];

        var y = observCol.GetGenericArguments();

        var instance = (Person)Activator.CreateInstance(x);

        Console.WriteLine(instance.Name); // Print Antonio!!!


来源:https://stackoverflow.com/questions/14639987/why-reflection-does-not-find-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!