Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why reflection does not find property

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:

enter image description here

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

like image 300
Tono Nam Avatar asked Jul 02 '26 03:07

Tono Nam


2 Answers

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();
like image 74
Ani Avatar answered Jul 04 '26 16:07

Ani


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!!!
like image 28
Frank59 Avatar answered Jul 04 '26 16:07

Frank59



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!