【发布时间】:2013-02-24 19:50:55
【问题描述】:
我对这个错误感到很困惑:
Cannot implicitly convert type 'System.Func<T,T,T> [c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll]' to 'System.Func<T,T,T> [c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll]' path\to\my\project\Operators.cs
类型相同,为什么还要进行强制转换?代码如下:
public static class Operators<T>
{
private static Func<T,T,T> _add = null;
public static T Add<T>(T a, T b)
{
if (_add == null) {
var param1Expr = Expression.Parameter(typeof (T));
var param2Expr = Expression.Parameter(typeof (T));
var addExpr = Expression.Add(param1Expr, param2Expr);
var expr = Expression.Lambda<Func<T, T, T>>(addExpr, param1Expr, param2Expr);
_add = expr.Compile(); // <--- error occurs here
}
return _add.Invoke(a, b);
}
}
【问题讨论】:
-
你应该从编译器那里得到一个警告。你没有吗?
-
@EricLippert:有一个警告,但我暂时关闭了它们,因为我正在重构一堆东西,并且收到太多关于未使用变量的警告。
标签: c# generics casting delegates