I want to to compare typeof(IEnumerable<>) to the types of various specific classes of IEnumerable, e.g.
Compare(typeof(IEnumerable<>), typeof(IEnumerable<object>)) // should return true
Compare(typeof(IEnumerable<>), typeof(IEnumerable<int>)) // should return true
Compare(typeof(IEnumerable<>), typeof(IEnumerable<MyClass>)) // should return true
Compare(typeof(IEnumerable<>), typeof(IEnumerable)) // should return FALSE because IEnumerable is not the generic IEnumerable<> type
How can I do this? All common methods such as == or IsAssignableFrom return false for all above examples.
Probably not necessary for the question, but some background:
I'm writing a conversion class that convers an object into some other type. I'm using Attributes (XlConverts):
public class XlConvertsAttribute : Attribute
{
public Type converts;
public Type to;
}
to flag which type each methods converts into. One of my conversion methods converts an object into IEnumerable:
[XlConverts(converts = typeof(object), to = typeof(IEnumerable<>))]
public static IEnumerable<T> ToIEnumerable<T>(object input)
{
// ....
}
Then I have a more general method
public static object Convert(object input, Type toType)
{
// ...
}
Which uses reflection to get the method that has XlConverts.to == toType, so basically it reflects its own class to find the approrpaite conversion method given the desired target type.
now when I call Convert(input, typeof(IEnumerable)), it is supposed to find the method ToIEnumerable by reflection. But as I can only flag it with [XlConverts(to = typeof(IEnumerable<>)), and IEnumerable<> is not IEnumerable, it won't find this method.
I'm aware just using IEnumerable instead of IEnumerable<> would do the job here, but I explicitly need to use the generic IEnumerable<> because later on, I want to do further reflection and filter out all methods that convert into a generic type.
Thanks!