views:

57

answers:

4

I've got a generic method:

Func<IEnumerable<T>, bool> CreateFunction<T>()

where T can be any number of different types. This method does a bunch of stuff using reflection and if T is an IDictionary, regardless of the the dictionary's TKey and TValue I need to execute dictionary specific code.

So the method could be called:

var f = CreateFunction<string>();
var f0 = CreateFunction<SomePocoType>();
var f1 = CreateFunction<IDictionary<string,object>>();
var f2 = CreateFunction<Dictionary<string,object>>();
var f3 = CreateFunction<SomeDerivedDictionaryType<string,object>>();

etc.

Clarification per @Andy's answer

Ultimately I want to know if T inherits from/implements IDictionary even if T itself is Dictionary or some other type that derives from that interface.

if(typeof(T) == typeof(IDictionary<,>)

doesn't work because T is the generic type not the generic type definition.

And without knowing TKey and TValue (which are not known at compile time) I can't do a comparison to any concrete type that I would know about until runtime.

The only thing that I've come up with are looking at the type's name or inspecting its method with reflection, looking for methods that would lead me to believe it is a dictionary (i.e. look for ContainsKey and get_Item).

Is there any straightforward way to make this sort of determination?

A: 

I think that if you call Type.GetGenericTypeDefinition() that should return the "base" generic type used to construct the concrete Type.

Note that just comparing this to IDictionary<,> is likely not enough, because if someone passes in an instance of Dictionary<,> I assume you would want to use that, as well. You could either check to see if the Type implements IDictionary<,> or you might be able to call Type.IsAssignableFrom(), although based on the doc I'm not sure how well this would work with generic Types.

Andy
Tried IsAssignableFrom and you are right it doesn't work with the type def. And you are right, part of the problem is knowing that an instance of T implements the IDictionary interface, not whether T is that interface type.
dkackman
+1  A: 

You could do something like

class Program
{
    static void Main(string[] args)
    {
        Example<IDictionary<int, string>>.IsDictionary();

        Example<SortedDictionary<int, string>>.IsDictionary();

        Example<Dictionary<int, string>>.IsDictionary();

        Console.ReadKey();
    }
}

public class Example<T>
{
    public static void IsDictionary()
    {
        if (typeof(T).GetInterface(typeof(IDictionary<,>).Name) != null || typeof(T).Name.Contains("IDictionary"))
        {
            Console.WriteLine("Is IDictionary");
        }
        else
        {
            Console.WriteLine("Not IDictionary");
        }
    }
}
Jamie Altizer
typeof(IDictionary) doesn't compile and unfortunately no type can actually implement IDictionary<,> (without generic arguments) so it goes back to looking for the type name.
dkackman
To compile you would have to add "using System.Collections;" because IDictionary is the interface for Dictionary. There are two interfaces you wish to check for, IDictionary and IDictionary<,>. The results of the code provided are correct and identify "Is IDictionary."
Jamie Altizer
The front side of the if statement validates the concrete types that would support IDictionary or IDictionary<,>, the latter evaluates a T defined as the interface you are probing.
Jamie Altizer
Doh - It's been so long I forgot all about the non-generic collection namespace!
dkackman
Does not work for non-dictionary types that happen to contain "IDictionary", such as: Example<IDictionaryService>.IsDictionary(). See my answer below.
Nathan Baulch
+1  A: 

The easy way is just this:

Type iDict = null;
if (typeof(T).GetGenericTypeDefinition() == typeof(IDictionary<,>))
    iDict = typeof(T);
else
    iDict = typeof(T).GetInterface(typeof(IDictionary<,>).Name);
if (iDict != null)
{
    var genericParams = iDict.GetGenericArguments();
    Type tKey = genericParams[0], tValue = genericParams[1];
}

Note that this will not work (throws an exception) if T implements more than one IDictionary<,> interface, but that will probably be fine for your purposes.

For the sake of completeness, here's an implementation that will work on types with multiple IDictionary<,> interfaces by using the first one:

Type iDict = t.GetType().GetInterfaces()
              .Where(t => t.IsGenericType
               && t.GetGenericTypeDefinition() == typeof(IDictionary<,>))
              .FirstOrDefault();
if (iDict != null)
{
    var genericParams = iDict.GetGenericArguments();
    Type tKey = genericParams[0], tValue = genericParams[1];
}

Note that in this second routine t is an object, whereas T is a type in the first routine.

Gabe
Works for derived dictionaries (Dictionary, SortedDictionary) but not when <t> is itself IDictionary.
dkackman
dkackman: Sorry, I was operating under the assumption that `T` was an object. I changed it to work for where `T` is a type.
Gabe
A: 

You can avoid using ugly and potentially risky type name string checking using the IsGenericType and GetGenericTypeDefinition members, as follows:

var type = typeof (T);
if (typeof (IDictionary).IsAssignableFrom(type))
{
    //non-generic dictionary
}
else if (type.IsGenericType &&
         type.GetGenericTypeDefinition() == typeof (IDictionary<,>))
{
    //generic dictionary interface
}
else if (type.GetInterfaces().Any(
            i => i.IsGenericType &&
                 i.GetGenericTypeDefinition() == typeof (IDictionary<,>)))
{
    //implements generic dictionary
}
Nathan Baulch