Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if an object's type implements IEnumerable<X> where X derives from Base using Reflection

Give a base class Base, I want to write a method Test, like this:

private static bool Test(IEnumerable enumerable)
{
...
}

such that Test returns true if the type of o implements any interface of IEnumerable<X> where X derives from Base, so that if I would do this:

public static IEnumerable<string> Convert(IEnumerable enumerable)
{
    if (Test(enumerable))
    {
        return enumerable.Cast<Base>().Select(b => b.SomePropertyThatIsString);
    }

    return enumerable.Cast<object>().Select(o => o.ToString());
}

...that it would do the right thing, using Reflection. I'm sure that its a matter of walking across all the interfaces of the type to find the first that matches the requirements, but I'm having a hard time finding the generic IEnumerable<> among them.

Of course, I could consider this:

public static IEnumerable<string> Convert(IEnumerable enumerable)
{
    return enumerable.Cast<object>().Select(o => o is Base ? ((Base)o).SomePropertyThatIsString : o.ToString());
}

...but think of it as a thought experiment.

like image 737
Dave Van den Eynde Avatar asked Mar 30 '10 18:03

Dave Van den Eynde


1 Answers

You could also use a LINQ query that could look like this.

public static bool ImplementsBaseType(IEnumerable objects)
{
    int found = ( from i in objects.GetType().GetInterfaces()
                 where i.IsGenericType && 
                       i.GetGenericTypeDefinition() == typeof(IEnumerable<>) &&
                       typeof(MyBaseClass).IsAssignableFrom(i.GetGenericArguments()[0])
                 select i ).Count();

    return (found > 0);
}

This code assumes the following using statements:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Since this is just a thought experiment. Here is another implementation as an extension method.

public static class ConversionAssistants
{
    public static bool GenericImplementsType(this IEnumerable objects, Type baseType)
    {
        foreach (Type type in objects.GetType().GetInterfaces())
        {
            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
                {
                    if (baseType.IsAssignableFrom(type.GetGenericArguments()[0]))
                        return true;
                }
            }
        }
        return false;
    }
}
like image 55
VoidDweller Avatar answered Nov 09 '22 00:11

VoidDweller