T,T,T>不能从Func键<转换;到Func键< T,T,T> [英] Cannot convert from Func<T,T,T> to Func<T,T,T>
问题描述
我挺这个错误困惑:
无法隐式转换类型'System.Func<吨,T,T> [C:\Program文件(x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll]'到'System.Func< T,T,T> [C:\Program文件(x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll]'path\to\my\project\Operators。 CS
块引用>
该类型是相同的,为什么它甚至试图做一个投?下面的代码:
公共静态类运营商< T>
{
私有静态Func键< T,T,T> _add = NULL;
公共静态不要再增加< T>(T A,T B)
{
如果(_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键LT; T,T,T>>(addExpr,param1Expr,param2Expr);
_add = expr.Compile(); //< ---在这里出现错误
}
返回_add.Invoke(A,B);
}
}
解决方案的问题是,你的方法的是通用的,引入新的类型参数
T
。因此,T
之外的方法是不一样的T
方法内。
只要改变你的方法不能通用:
公共静态不要再增加(T一, T b)
...这应该罚款。
要更清楚,你的代码目前的相当于的这样:
公开静态类符< TC>
{
私有静态Func键< TC,TC,TC> _add = NULL;
公共静态TM添加< TM>(TM A,TM B)
{
如果(_add == NULL){
VAR param1Expr = Expression.Parameter( typeof运算(TM));
VAR param2Expr = Expression.Parameter(typeof运算(TM));
VAR addExpr = Expression.Add(param1Expr,param2Expr);
VAR EXPR = Expression.Lambda<&Func键LT,TM,TM,TM>>
(addExpr,param1Expr,param2Expr);
_add = expr.Compile();
}
返回_add.Invoke(A,B);
}
}
请注意我是如何改名为
T
的类到TC
和T $ C介绍$ C>的方法的介绍
TM
。错误消息现在看起来比较合理的:test.cs中(19,20):错误CS0029:无法隐式转换类型
'System.Func< TM,TM,TM>'到'System.Func< TC,TC,TC>'
I'm quite confused by this error:
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
The types are identical, why is it even trying to do a cast? Here's the code:
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); } }
解决方案The problem is that your method is generic, introducing a new type parameter
T
. So theT
outside the method isn't the same as theT
inside the method.Just change your method to not be generic:
public static T Add(T a, T b)
... and it should be fine.
To be clearer, your code is currently equivalent to this:
public static class Operators<TC> { private static Func<TC, TC, TC> _add = null; public static TM Add<TM>(TM a, TM b) { if (_add == null) { var param1Expr = Expression.Parameter(typeof(TM)); var param2Expr = Expression.Parameter(typeof(TM)); var addExpr = Expression.Add(param1Expr, param2Expr); var expr = Expression.Lambda<Func<TM, TM, TM>> (addExpr, param1Expr, param2Expr); _add = expr.Compile(); } return _add.Invoke(a, b); } }
Note how I've renamed the
T
introduced by the class toTC
, and theT
introduced by the method toTM
. The error message now looks more reasonable:Test.cs(19,20): error CS0029: Cannot implicitly convert type 'System.Func<TM,TM,TM>' to 'System.Func<TC,TC,TC>'
这篇关于T,T,T>不能从Func键<转换;到Func键< T,T,T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!