为什么这段代码会抱怨“泛型类型定义的数量”?
我有一个泛型类型:
class DictionaryComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>>
和一个工厂方法,它将(应该)为给定的字典类型创建此类的实例。
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);
}
去掉所有无关的东西 - 即使这段代码也会抛出相同的异常。
private static object CreateDictionaryComparer()
{
Type def = typeof(DictionaryComparer<,>);
Type t = def.MakeGenericType(new Type[] { typeof(String), typeof(object) });
return Activator.CreateInstance(t);
}
断言通过了,所以我知道 T
是通用的并且有两个通用参数。但是,带有 MakeGenericType
的行除外:
提供的泛型参数数量不等于泛型类型定义的数量。
参数名称:实例化
我过去做过这种事情,但我一生都无法弄清楚为什么这在这种情况下不起作用。 (另外我还得谷歌arity)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了。
我将
DictionaryComparer
声明为内部类。我只能假设MakeGenericType
想要创建Query.DictionaryComparer
并且没有提供T
。失败代码
Figured it out.
I had
DictionaryComparer
declared as an inner class. I can only assume thatMakeGenericType
wanted to make aQuery<T>.DictionaryComparer<string,object>
and was not providedT
.Failing code
CLR 为应用程序使用的每种类型创建一个内部数据结构。这些数据结构称为类型对象。具有泛型类型参数的类型称为开放类型,CLR 不允许构造开放类型的任何实例(类似于 CLR 如何阻止构造接口类型的实例) )。
更改
于
CLR creates an internal data structure for each and every type in use by an application.These data structures are called type objects. A type with generic type parameters is called an open type, and the CLR does not allow any instance of an open type to be constructed (similar to how the CLR prevents an instance of an interface type from being constructed).
Change
on the