没有为类型“System.Int32”定义二元运算符 Multiply;和“System.Double”。
为什么下面的代码在运行时会抛出异常,而用传统方式编译却没有问题?
var left = Expression.Constant(25d);
var right = Expression.Constant(20);
// Throws an InvalidOperationException!
var multiplyExpression = Expression.Multiply(left, right);
var multiply = 25d * 20;
Debug.WriteLine(multiply.ToString()); // Works normally!
我不会使用 Expression.Convert
因为我无法准确确定应转换哪个表达式。
Why the following code throws an exception at runtime, whereas doing it in the traditional way compiles without problem?
var left = Expression.Constant(25d);
var right = Expression.Constant(20);
// Throws an InvalidOperationException!
var multiplyExpression = Expression.Multiply(left, right);
var multiply = 25d * 20;
Debug.WriteLine(multiply.ToString()); // Works normally!
I won't use Expression.Convert
since I can't determine exactly which expression should be converted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我想出了如何使用 TypeCode 枚举来解决问题,以确定哪个节点具有更高的类型精度,然后将后一个节点的类型转换为前一个节点的类型,反之亦然:
Well, I figured out how to solve the problem using TypeCode enumeration to determine which node would have higher type precision, then convert the latter node's type to the former's type, and vice versa:
或者,如果您不知道左侧具有更高的精度,并且您希望始终得到
double
结果,您可以这样说:Or, if you don't know that the left side has higher precision, and you want to always end up with a
double
result, you could say something like:那么您标题中的错误消息告诉您为什么会出现异常。
没有定义将 System.Int32 和 System.Double 作为参数的
Expression.Multiply
方法。*
会起作用,因为它的级别较低,并且您的值将自动进行类型转换。Well the error message in your title is telling you why there is an exception.
There is no
Expression.Multiply
method defined which takes aSystem.Int32
and aSystem.Double
as arguments.The
*
will work because it's lower level and your values will be type cast automatically.