Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a generic type is built from a specific generic type definition?

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?

like image 791
dkackman Avatar asked Dec 13 '22 19:12

dkackman


1 Answers

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
}
like image 70
Nathan Baulch Avatar answered Dec 15 '22 08:12

Nathan Baulch