I've got a generic type:
class DictionaryComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>>
And a factory method that will (should) create an instance of this class for a given dictionary type.
private static IEqualityComparer<T> CreateDictionaryComparer<T>()
{
Type def = typeof(DictionaryComparer<,>);
Debug.Assert(typeof(T).IsGenericType);
Debug.Assert(typeof(T).GetGenericArguments().Length == 2);
Type t = def.MakeGenericType(typeof(T).GetGenericArguments());
return (IEqualityComparer<T>)Activator.CreateInstance(t);
}
Stripping away all of the extraneous stuff - even this code throws the same exception.
private static object CreateDictionaryComparer()
{
Type def = typeof(DictionaryComparer<,>);
Type t = def.MakeGenericType(new Type[] { typeof(String), typeof(object) });
return Activator.CreateInstance(t);
}
The Asserts pass so I know that T
is generic and has two generic arguments. The line with MakeGenericType
however excepts with:
The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation
I've done this sort of thing in the past and for the life of me can't figure out why this isn't working in this case. (plus I had to Google arity).
T
toCreateDictionaryComparer
? I've tried passingCreateDictionaryComparer<IDictionary<string, string>>()
and this works fine for me (using Mono C# compiler version 1.9.1.0). – Jocelynjocelyne