Is this is an ExpressionTrees bug? #2

前端 未结 2 1648
暗喜
暗喜 2021-02-14 07:47

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 en

2条回答
  •  太阳男子
    2021-02-14 08:37

    Not a real answer yet, I'm investigating, but the first line is compiled as:

    Func converter1 = x => (ConsoleColor)(int)x;
    

    If you try to create an expression from the previous lambda, it will work.

    EDIT : In the C# spec, §6.2.2, you can read:

    An explicit enumeration conversion between two types is processed by treating any participating enum-type as the underlying type of that enum-type, and then performing an implicit or explicit numeric conversion between the resulting types. For example, given an enum-type E with and underlying type of int, a conversion from E to byte is processed as an explicit numeric conversion (§6.2.1) from int to byte, and a conversion from byte to E is processed as an implicit numeric conversion (§6.1.2) from byte to int.

    So explicit casts from enum to decimal are handled specifically, that's why you get the nested casts (int then decimal). But I can't see why the compiler doesn't parse the lambda body the same way in both cases.

提交回复
热议问题