Expression.Convert不会引发InvalidOperationException异常的不变量类型参数? [英] Expression.Convert doesn't throw InvalidOperationException for invariant value type parameters?
问题描述
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的
。
这似乎整个方差逻辑不正确支持。它正确地抱怨不能够从
Func键<转换; SOMETYPE>
到Func键< SomeOtherType>
,但它不 ŧ抱怨从Func键<转换;对象>
到Func键<字符串方式>
有趣的是,一旦
SOMETYPE
和SomeOtherType
在同一个类层次结构(SomeOtherType
从SOMETYPE
),它永远不会抛出异常。如果不是,它的作用。
解决方案
这是一个错误吗?
块引用>
是的。表达式树库可能不是始终贯穿更新,当我们添加协变和逆变。我们对此深感抱歉。
我报告说,它作为微软连接错误。
块引用>
谢谢!有人会看看它吧。
如何正确检查类型是否可以被转换为另一种类型?
块引用>
现在的问题是含糊不清。给定两个类型的对象,你想知道的:
- 是否.NET运行时认为类型分配兼容
- 请问C#编译器认为有各类型之间的隐式转换?
- 请问C#编译器认为有各类型之间的显式转换?
INT和短,例如不分配由.NET规则相一致。 int是显式转换而不是隐式转换为短,短既含蓄,并明确转换成int,用C#的规则。
Expression.Convert
generally throws inInvalidOperationException
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 callingExpression.Convert
. The expression tree compiles correctly, but when I call the created delegate, I get an expectedInvalidCastException
.
- Is this a bug? (I reported it as a bug on Microsoft Connect.)
- 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>
toFunc<SomeOtherType>
, but it doesn't complain about converting fromFunc<object>
toFunc<string>
.Interestingly, once
SomeType
andSomeOtherType
are in the same class hierarchy (SomeOtherType
extends fromSomeType
), 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屋!