Exercise Practice of Expression Tree: Variables, Constants and Assignment

Defining variables

Parameter Expression is used to create variable and variable parameter expressions.

In C #, variables are divided into the following types:

  • Value types
  • Reference types
  • Pointer types

Generally, only value types and reference types are used, and pointer types are not mentioned here.

The basic value types of C # are bool, byte, char, double, float, int, long, etc. (In C #, arrays belong to the reference type).

The expression tree creates a variable in two ways:

            ParameterExpression varA = Expression.Variable(typeof(int), "x");
            ParameterExpression varB = Expression.Parameter(typeof(int), "y");

Difference:

Expression.Variable() denotes the creation of a variable;

Expression.Parameter() represents the creation of an incoming parameter;

As for the use of differences, there will be many specific examples to understand later.

The same thing: The types generated are Parameter Expressions.

Example:

            int a; 

            ParameterExpression varA = Expression.Variable(typeof(int), "x");
        static void Main(string[] args)
        {
            // Equivalent to int b in Test()
            ParameterExpression varB = Expression.Parameter(typeof(int), "y");
            Console.ReadKey();
        }
        public static void Test(int b)
        {
            Console.WriteLine(b);
        }

Reference types also use the same method to create variables.

An example method for reference types will be used later.

Const

Define a constant using Expression.Constan().

Example:

            ConstantExpression constant = Expression.Constant(100);
            ConstantExpression constant1 = Expression.Constant(100, typeof(int));

It is recommended to use overloading method with two parameters, so that the browsing code can be quickly understood and easy to find and modify.

assignment

Expression.Assign() is used to assign values to expression tree variables.

Common definitions are as follows

BinaryExpression Assign(Expression left, Expression right);

Give the value of the right expression to the left expression.

Assignment of variables:

            ParameterExpression a = Expression.Variable(typeof(int), "x");

            ConstantExpression constant = Expression.Constant(100, typeof(int));

            BinaryExpression assign = Expression.Assign(a, constant);

Note the type of overloading method

Console's common overloading methods are

        public static void WriteLine(object value);

        public static void WriteLine(float value);

        public static void WriteLine(string value);

When using expression tree, pay attention to the overloaded method to be invoked, which can not be misled by the implicit transformation of normal code.

            int a = 100;
            Console.WriteLine(a);

            ParameterExpression aa = Expression.Parameter(typeof(int), "a");
            BinaryExpression aaa = Expression.Assign(aa, Expression.Constant(100, typeof(int)));
            MethodCallExpression method = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), aa);

            // If you haven't learned how to execute an expression tree, you can ignore it now.
            var call = Expression.Block(new ParameterExpression[] { aa }, aaa, method);
            Expression<Action> lambda = Expression.Lambda<Action>(call);
            lambda.Compile()();

In the previous output variable a, the system performs an implicit type conversion. But when using expression tree to call methods, the corresponding type is needed to find the correct overloading method. Calling Console.WriteLine() from the expression tree above will result in the following error:

System.ArgumentException:"Expression of type 'System.Int32' cannot be used for parameter of type 'System.String' of method 'Void WriteLine(System.String)'
Arg_ParamName_Name"

Keywords: C# Lambda

Added by kundan on Mon, 16 Sep 2019 17:40:36 +0300