mardi 13 octobre 2015

Determine with reflection if a class property is an object that inherits List

This question already has an answer here:

I am needing to use reflection to filter out all object properties that are collections. Now, they are not just Generic list always, but a class that inherits a List type. So as an example, I have class foo(). A property of class foo() is the class fooCollection(). FooColection looks like the following:

    public class foo
    {
         public FooCollection myCollection {get;set;}
    }


    public class fooCollection : List<Foo>
    {



    }

I do not know what to expect as the type of List, but only need to know if it is a collection. How can I find this out? I asked a prior question, but worded it differently, and the results were not working with my particular instance.

I have tried exampels such as the following where i look through all properties of foo, and try to find the property that is fooColelction:

typeof(IEnumerable<>).IsAssignableFrom(prop.PropertyType)

This is not working properly for me though. The type is not IEnumerable, though it is a collection. It inherits an IEnumerable type. Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire