lundi 12 octobre 2015

Trying to find all collections in an object not null with reflection

I am trying to build a generic unit test method, to make sure all of our objects are initiliazing their properties that are collections on the constructor. I instead of manually checking with assert on each property, and then thinking ahaed if a class object is modified, I wanted to use reflection. The problem is that the collections, and not just List, but a class copy of the class, and then inherit List.

So, we may have somthing like the following:

public class PaymentType
{
   int Id{get;set;}
   PaymentTypeCollection PaymentTypeCollection {get;set;}

}

public class PaymentTypeCollection : List<PaymentType>
{
   //som ecustom stuff here

}

Now the problem is that when you itterate over the properties of the object from class PaymentType, and try to do a reflection method with the following check, it comes back false, and understandably.

foreach (var prop in objectType.GetProperties())
        {
            // test if property is IEnumerable<T>? This comes back as false
            if (typeof(IEnumerable<>).IsAssignableFrom(prop.PropertyType))
            {
               //do stuff here
            }
        }

How could I go about the issue. I need to see if the property, is then inheriting IEnumerable? Since the type would actually be PaymentTypeColelction. How would I handle that?

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire