vendredi 11 août 2017

C# reflection find property which type inherits some other type

I'm wondering how to use the reflection mechanism in the following scenario:

public class A { }
public class B { }

public class ListA : ICollection<A> { }

public class ListB : ICollection<B> { }

public class Container
{
    public ListA LA { get; set; }
    public ListB LB { get; set; }
}

then I want to find a property, which type inherits the type ICollection<B>

var container = new Container();

var found = container.GetType().GetProperties().FirstOrDefault(x => x.PropertyType == typeof(ICollection<B>));

and of course the found variable is null, so how to move deeper with the reflection?





Aucun commentaire:

Enregistrer un commentaire