【问题标题】:Is this is an ExpressionTrees bug? #4这是一个表达式树大吗? #4
【发布时间】:2009-11-22 02:48:35
【问题描述】:

这不是“接近真实”的情况,但它表明 Expressions API 如果在源类型中找到合适的运算符,则不会在目标转换类型中查找运算符。

sealed class Foo
{
  public static explicit operator Bar(Foo foo) { return null; }
}

sealed class Bar
{
  public static implicit operator Bar(Foo foo) { return null; }
}

class Program
{
  public static void Blah(Bar bar) { }

  private static void Main()
  {
    Expression<Func<Foo, Bar>> expr1 = x => x;

    // fine, used implicit operator from Bar

    var param = Expression.Parameter(typeof(Foo), "x");

    var expr2 = Expression.Lambda<Func<Foo, Bar>>(
      Expression.Convert(param, typeof(Bar)),
      param);

    // fine, but used explicit operator from Foo!
  }
}

另外,如果模拟两种类型的用户定义运算符之间的歧义,C# 编译器根本不编译转换,但 Expressions API 将使用转换源类型中的运算符。

【问题讨论】:

    标签: c# .net expression-trees


    【解决方案1】:

    不,这不是表达式树错误。如果您无法为表达式树库提供足够的信息来执行您想要的操作,那么您只需要接受它的默认值即可。这些默认设置没有任何理由需要遵循 C# 的规则;表达式树库不是 C# 编译器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多