我不明白为什么无法推断出对GenericMethod<T>()
的调用。我的实际问题包含更多通用参数,从而使调用代码非常冗长且难以阅读。
GenericMethod<T>()
可以在没有显式类型参数的情况下被调用,但是约束不适用于委托,并且一旦您输入了受约束的类型参数,就回到第一个平方。
delegate void GenericDelegate<T>(T t);
static void GenericMethod<T>(GenericDelegate<T> _) { }
static void IntMethod(int _) { }
static void CallingMethod()
{
// The type arguments for method 'GenericMethod<T>(GenericDelegate<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
GenericMethod(IntMethod);
}
还有另一种方法可以使调用代码变得像这里描述的那样简单吗? GenericMethod<T>()
是内部库的一部分。
我不明白为什么不能推断出对
GenericMethod<T>()
的调用。
实现起来可能很昂贵(时间,资源)。
delegate void GenericDelegate<T>(T t);
static void GenericMethod<T>(GenericDelegate<T> method) { }
static void Method(int _) { }
static void Method(float _) { }
static void CallingMethod()
{
GenericMethod(Method);
}
[方法组Method
具有两个重载时,编译器无法推断T
的类型。即使该方法组仅包含一个重载,问题实际上也完全相同!