Question

I have a method that accepts an IEnumerable<dynamic>

Is there a way to test the dynamic type inside the method to see whether or not it implements a particular interface?

My intention is something like this:

public void myMethod(IEnumerable<dynamicT> enumerable)
{
    if (dynamicT is IInterface)
    {
        // do one thing
    }
    else if (dynamicT is IAnotherInterface)
    {
        // do another thing
    }
}

Update

I should have included this important fact: <T> is dynamic.

Please forgive, it's approaching midnight from where I'm coding. :-$

Was it helpful?

Solution 2

The is keyword is for an object while reflection is needed for a type:

You can use typeof(T).GetInterfaces() to pull all the interfaces applied to a particular Type.

public void MyMethod(IEnumerable<T> enumerable)
{
    var typeInterfaces = typeof(T).GetInterfaces();

    if (typeInterfaces.Contains(typeof(IInterface))) {
        // Something
    }
    else if(typeInterfaces.Contains(typeof(IAnotherInterface))) {
        // Something Else
    }
}

============ Update Based on Comment ============

If T is dynamic you cannot get the information you're looking for from the Type itself since T is could represent any number of different types all at the same time.

You can however loop through each element and test the object itself using the is keyword.

public static void MyMethod<T>(IEnumerable<T> enumerable)
{
    foreach (var dynObj in enumerable)
    {
        var typeInterfaces = dynObj.GetType().GetInterfaces();

        if (typeInterfaces.Contains(typeof(IInterface))) {
            // Something
        }
        else if(typeInterfaces.Contains(typeof(IAnotherInterface))) {
            // Something Else
        }
    }
}

Or if you just want to test all possible interfaces in the enumerable you could do this:

public static void MyMethod<T>(IEnumerable<T> enumerable)
{
    var allInterfaces = enumerable.SelectMany(e => e.GetType().GetInterfaces()).ToList();

    if (allInterfaces.Contains(typeof(ITheFirstInterface)))
    {
        Console.WriteLine("Has The First Interface");
    }

    if (allInterfaces.Contains(typeof(ITheSecondInterface)))
    {
        Console.WriteLine("Has The Second Interface");
    }
}

OTHER TIPS

You can use Type.IsAssignableFrom method:

Determines whether an instance of the current Type can be assigned from an instance of the specified Type.

public void myMethod(IEnumerable<T> enumerable)
{
    if (typeof(IInterface).IsAssignableFrom(typeof(T))
    {
        // do one thing
    }
    else if (typeof(IAnotherInterface).IsAssignableFrom(typeof(T))
    {
        // do another thing
    }
}

Please try the following. Since, T is dynamic you need to peek the first item in the list to find out its type.

public void myMethod<T>(IEnumerable<T> enumerable)
{
    dynamic peek = enumerable.FirstOrDefault();
    Type dtype = peek.GetType();

    if (typeof(IInterface).IsAssignableFrom(dtype))
    {
        // do one thing
    }
    else if (typeof(IAnotherInterface).IsAssignableFrom(dtype))
    {
        // do another thing
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top