views:

280

answers:

2

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.

A: 

You can use the Type.FindInterfaces to filter out all the IEnumerable<> interfaces the type implements and check the generic parameters (through Type.GetGenericArguments) on each of them to see if it's Base or inherits from Base.

Update: Here's some sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication1
{
    class Base
    {
        string Prop { get; set;}
    }

    class A : Base
    {
    }

    class Test : List<A>
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Test test = new Test();
                Type myType = test.GetType();

                //string filterCriteria = "IEnumerable`1";
                Type typeA = Type.GetType("ConsoleApplication1.A");
                string filterCriteria = "System.Collections.Generic.IEnumerable`1[[" +
                                        typeA.AssemblyQualifiedName +
                                        "]]";

                // Specify the TypeFilter delegate that compares the 
                // interfaces against filter criteria.
                TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
                Type[] myInterfaces = myType.FindInterfaces(myFilter,
                    filterCriteria);
                if (myInterfaces.Length > 0)
                {
                    Console.WriteLine("\n{0} implements the interface {1}.",
                        myType, filterCriteria);
                    for (int j = 0; j < myInterfaces.Length; j++)
                        Console.WriteLine("Interfaces supported: {0}.",
                            myInterfaces[j].ToString());
                }
                else
                    Console.WriteLine(
                        "\n{0} does not implement the interface {1}.",
                        myType, filterCriteria);
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: " + e.Message);
            }
            catch (TargetInvocationException e)
            {
                Console.WriteLine("TargetInvocationException: " + e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }

        public static bool MyInterfaceFilter(Type typeObj, Object criteriaObj)
        {
            // This will be true, if criteria is
            // System.Collections.Generic.IEnumerable`1[[ConsoleApplication1.A, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
            if (typeObj.FullName == criteriaObj.ToString())
                return true;
            // This will be true, if criteria is
            // IEnumerable`1
            // You will still need to check the generic parameters on the original type
                // (generic parameters are not exposed on Type instances for interfaces
            else if (typeObj.Name == criteriaObj.ToString())
                return true;
            else
                return false;
        }
    }
}
Franci Penov
That's what i thought, but how do I check if an interface is an instance of the generic interface IEnumerable<>?
Dave Van den Eynde
+1  A: 

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;
    }
}
VoidDweller
instead of arg.BaseType == typeof(MyBaseClass), you could say typeof(MyBaseClass).IsAssignableFrom(arg), right?
Dave Van den Eynde
@Dave: Yep, that works too.
VoidDweller
IsAssignableFrom` is better, because `==` will work only if the type inherits *directly* from MyBaseClass
Thomas Levesque
@Thomas: Yeah, I like that better. Edited my answer to reflect that.
VoidDweller