这是一个曾经pressionTrees错误? #2这是一个、错误、pressionTrees

2023-09-04 02:07:38 作者:要走别再回头

看起来像前pressionTrees编译器应靠近与C#规范的许多行为,但不像C#有转换不支持十进制任何枚举类型

Looks like ExpressionTrees compiler should be near with the C# spec in many behaviors, but unlike C# there is no support for conversion from decimal to any enum-type:

using System;
using System.Linq.Expressions;

class Program
{
  static void Main()
  {
    Func<decimal, ConsoleColor> converter1 = x => (ConsoleColor) x;
    ConsoleColor c1 = converter1(7m); // fine

    Expression<Func<decimal, ConsoleColor>> expr = x => (ConsoleColor) x;

    // System.InvalidOperationException was unhandled
    // No coercion operator is defined between types
    // 'System.Decimal' and 'System.ConsoleColor'.

    Func<decimal, ConsoleColor> converter2 = expr.Compile();

    ConsoleColor c2 = converter2(7m);
  }
}

其他很少使用C#显式转换,如双 - &GT;枚举类型的存在和工作在C#规范的解释,而不是十进制 - &GT;枚举类型。这是一个错误?

Other rarely used C# explicit conversions, like double -> enum-type exists and works as explained in C# specification, but not decimal -> enum-type. Is this a bug?

推荐答案

这可能是一个错误,这可能是我的错。我们对此深感抱歉。

It is probably a bug, and it is probably my fault. Sorry about that.

获取十进制转换权是建立在EX pression树code编译器和运行时,因为正确的最困难的部分之一的十进制转换实际上是实现为用户自定义在运行时转换的,但由编译器处理为内置转换的。十进制是唯一的类型与此属性,因此也有各种特殊用途的齿轮在分析这些案件。其实,还有一个在分析处理可空枚举可空十进制转换的特殊情况下,所谓的IsEnumToDecimalConversion方法;一个相当复杂的特殊情况。

Getting decimal conversions right was one of the hardest parts of building the expression tree code correct in the compiler and the runtime because decimal conversions are actually implemented as user-defined conversions in the runtime, but treated as built-in conversions by the compiler. Decimal is the only type with this property, and therefore there are all kinds of special-purpose gear in the analyzer for these cases. In fact, there is a method called IsEnumToDecimalConversion in the analyzer to handle the special case of nullable enum to nullable decimal conversion; quite a complex special case.

赔率是好的,我没有考虑到某些情况下,走另一条路,并产生不良code结果。感谢您的笔记;我会送这关的测试团队,我们会看看我们是否能得到一个摄制去。赔率是好的,如果这确实变成是一个真正的错误,这将不是固定不变的C#4最初的版本;在这一点上,我们正在采取只臭虫的用户是由编译器触电,使该版本是稳定的。

Odds are good that I failed to consider some case going the other way, and generated bad code as a result. Thanks for the note; I'll send this off to the test team, and we'll see if we can get a repro going. Odds are good that if this does turn out to be a bona fide bug, this will not be fixed for C# 4 initial release; at this point we are taking only "user is electrocuted by the compiler" bugs so that the release is stable.