Expression.Convert不会引发InvalidOperationException异常的不变量类型参数? [英] Expression.Convert doesn't throw InvalidOperationException for invariant value type parameters?

查看:291
本文介绍了Expression.Convert不会引发InvalidOperationException异常的不变量类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Expression.Convert 一般罚全中,出现InvalidOperationException 时的不转换运算expression.Type和类型之间。



的返回类型参数的函数功能和LT;> 是协变的引用类型

  //这工作。 
Func键<&SOMETYPE GT;一个=()=>新SOMETYPE();
Func键<对象> B = A;



它的不是协变值类型的。




差异只适用于参考类型;如果你指定一个变量类型参数的值类型,该类型参数是不变的结果构造类型。




  //这不工作! 
Func键< INT> 5 =()=> 5;
Func键<对象> fiveCovariant =五类;



然而, Expression.Convert 认为,这是。可能的

  Func键< INT>回答=()=> 42; 
表达answerExpression = Expression.Constant(答案);
//没有出现InvalidOperationException在这行抛出。
表达转换
= Expression.Convert(answerExpression的typeof(Func键<对象>));

没有出现InvalidOperationException 打电话时被抛出 Expression.Convert 。表达式树编译正常,但是当我打电话创建的委托,我得到预期的 InvalidCastException的




  1. 这是一个错误? (我报作为微软连接的错误。)

  2. 如何正确检查类型是否可以被转换为另一种类型? 一些答案​​似乎是指使用转换。我非常喜欢它没有使用异常处理作为逻辑的方法。






这似乎整个方差逻辑不正确支持。它正确地抱怨不能够从 Func键<转换; SOMETYPE> Func键< SomeOtherType> ,但它不 ŧ抱怨从 Func键<转换;对象> Func键<字符串方式>



有趣的是,一旦 SOMETYPE SomeOtherType 在同一个类层次结构( SomeOtherType SOMETYPE ),它永远不会抛出异常。如果不是,它的作用。


解决方案

这是一个错误吗?




是的。表达式树库可能不是始终贯穿更新,当我们添加协变和逆变。我们对此深感抱歉。




我报告说,它作为微软连接错误。




谢谢!有人会看看它吧。




如何正确检查类型是否可以被转换为另一种类型?




现在的问题是含糊不清。给定两个类型的对象,你想知道的:




  • 是否.NET运行时认为类型分配兼容

  • 请问C#编译器认为有各类型之间的隐式转换?

  • 请问C#编译器认为有各类型之间的显式转换?



INT和短,例如不分配由.NET规则相一致。 int是显式转换而不是隐式转换为短,短既含蓄,并明确转换成int,用C#的规则。


Expression.Convert generally throws in InvalidOperationException when "No conversion operator is defined between expression.Type and type."

The return type parameter of Func<> is covariant for reference types.

// This works.
Func<SomeType> a = () => new SomeType();
Func<object> b = a;

It isn't covariant for value types.

Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type.

// This doesn't work!
Func<int> five = () => 5;
Func<object> fiveCovariant = five;

However, Expression.Convert believes it is possible.

Func<int> answer = () => 42;
Expression answerExpression = Expression.Constant( answer );
// No InvalidOperationException is thrown at this line.
Expression converted 
    = Expression.Convert( answerExpression, typeof( Func<object> ) );

No InvalidOperationException is thrown when calling Expression.Convert. The expression tree compiles correctly, but when I call the created delegate, I get an expected InvalidCastException.

  1. Is this a bug? (I reported it as a bug on Microsoft Connect.)
  2. How to properly check whether a type can be converted to another type? Some answers seem to refer to using Convert. I would very much prefer a method which doesn't have to use exception handling as logic.


It seems the entire variance logic isn't properly supported. It correctly complains about not being able to convert from Func<SomeType> to Func<SomeOtherType>, but it doesn't complain about converting from Func<object> to Func<string>.

Interestingly, once SomeType and SomeOtherType are in the same class hierarchy (SomeOtherType extends from SomeType), it never throws the exception. If they aren't, it does.

解决方案

Is this a bug?

Yes. The expression tree library was probably not updated consistently throughout when we added covariance and contravariance. Sorry about that.

I reported it as a bug on Microsoft Connect.

Thanks! Someone will have a look at it then.

How to properly check whether a type can be converted to another type?

The question is vague. Given two type objects, do you want to know:

  • Does the .NET runtime think the types are assignment compatible?
  • Does the C# compiler think that there is an implicit conversion between the types?
  • Does the C# compiler think that there is an explicit conversion between the types?

"int" and "short" for example are not assignment compatible by .NET rules. Int is explicitly convertible but not implicitly convertible to short, and short is both implicitly and explicitly convertible to int, by C# rules.

这篇关于Expression.Convert不会引发InvalidOperationException异常的不变量类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆