Have a very interesting situation and can't seem to find anything concrete on the webs surrounding reflection, inheritance and generics.
To start off with:
I have a base abstract classed, called:
public abstract class ItemBase
{
// default properties
}
ItemBase then becomes my super-class to an abstract entity called Item:
public abstract class Item
{
// some extra properties
}
The idea behind this is to construct entities that will inherit from Item with additional properties set:
public partial class City : Item {}
Now, in a test method I'm trying to use reflection to invoke a method on the passed entity and return whatever data, in what ever form, back from the method.
Assuming, for testing purposes we have a method in State, that returns a collection of all major cities:
public List<City> GetAllMajorCities() {}
In my stubs I might have a test to get all cities:
List<Item> cities = this.InvokeGenericMethod<Item>("State", "GetAllMajorCities", args);
Then the InvokeGenericMethod(string, string, object[] args) looks something like:
public IEnumerable<T> InvokeGenericMethod<Item>(string className, string methodName, object[] args)
{
Type classType = className.GetType(); // to get the class to create an instance of
object classObj = Activator.CreateInstance(classType);
object ret = classType.InvokeMember(methodName, BindingFlags.InvokeMethod, null, classObj, args);
}
In theory, from the example given, the value of "ret" should be a type of IEnumerable, however, the returning type, if added to a watch to investivage, return List, if I check the ret value as:
if (ret is IEnumerable<T>)
it ignores the check.
If I change
public List<City> GetAllMajorCities() {}
to
public List<ItemBase> GetAllMajorCities() {}
It seems to adhere to the inheritance and allow me to cast it to IEnumberable.
I'm completely stumped here.
Any ideas, recommendations would be greatly appreciated.
Thanks,
Eric