locked
Assignments in Expression Trees? RRS feed

  • Question

  • Why are there no assignments of properties allowed in expression trees? It is really a method call to the set method of the property so it seems as if it should be allowed.?

    Code Snippet

    class Program
    {
        static void Main(string [] args)
        {
            Expression<Action<P>> a = p => p.Number = 2; // error
            Expression<Action<P>> aa = p => p.SetNumber( 2 ); // works
        }
    }

    public class P
    {
        public int Number { get; set; }
        public void SetNumber( int n ) { Number = n; }
    }



    Schrock.
    Monday, April 23, 2007 7:32 PM

Answers

  • Nicholas -

     

    There is no technical reason why assignments are not supported in Expression Trees.  For Orcas though, we focused on encoding a subset of the language which could express state-insensitive operations.  This was because in many use-cases of Expression Trees, the ultimate execution environment of the tree can't understand assignments to state - as the state is stored back in the CLR objects.  In contrast, stateless expression trees can in principle be interpreted in a broader variety of settings.

     

    However, as we've seen more adoption of expression trees in other APIs, we've seen many uses where a more complete representation of the language would be useful.  This would include both assignments and support for statements.  We're actively considering adding this support post-Orcas.

     

    Thanks,

    Luke Hoban

    Visual C# Compiler Program Manager

    Thursday, April 26, 2007 1:44 AM