views:

582

answers:

1

Expressions class should be more accurate while searching for user-defined operators?

sealed class Foo
{
  // just the private static method!
  private static int op_Implicit() { return 1; }

  public static implicit operator int(Foo foo) { return 2; }
}

public class Program
{
  private static void Main()
  {
    var param = Expression.Parameter(typeof(Foo), "x");

    // IndexOutOfRangeException was unhandled!
    var lambda = Expression.Lambda<Func<Foo, int>>(
      Expression.Convert(param, typeof (int)), param);
  }
}

Also it's possible to define static op_Explicit or op_Implicit method with more than one arguments and Expression class accepts this method as an user-defined operator!

p.s. ExpressionTrees looking for operators with BindingFlags.NonPublic flag, it's allows to search operators in the user types, witch not directly visible for System.Core.dll, but it also allows the Expressions API to find private methods that "looks like" user-defined operators. But C# rules doesn't allows you to define and use non-public operators! I think the behavior of Expressions API should be the same...

A: 

This is not a bug.

FlowControl
This is not an answer.
RaphaelSP