Creating an expression tree that uses a dynamicall

2019-06-27 01:26发布

问题:

I have a fully initialized MethodBuilder and EnumBuilder. The MethodBuilder points to the entry point of a dynamic assembly. It has the following signature:

public static int Main (string [] args) {...}

The assembly generation code works fine and I can use Reflection.Emit to test it. Instead of emitting IL, I want to save the target code from an expression tree. It should:

  • declare a enum variable
  • assign it a value
  • write to the console
  • read pause the console
  • return a enum value as an Int32

EXPRESSION TREE:

// Intention: Declare string [] args in the expression scope.
var arguments = Expression.Parameter(typeof(string []), "args");
// Intention: var code = default(MyDynamicEnum);
var code = Expression.Variable(builderEnum, "code");
// Intention: code = MyDynamicEnum.Two;
var assign = Expression.Assign(code, Expression.Constant(2, builderEnum));
// Intention: Console.WriteLine(args [0]);
var write = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type [] { typeof(string) }), Expression.ArrayIndex(arguments, Expression.Constant(0, typeof(int))));
// Intention: Console.ReadKey(true);
var read = Expression.Call(typeof(Console).GetMethod("ReadKey", new Type [] { typeof(bool) }), Expression.Constant(true, typeof(bool)));
// Intention: return ((int) code);
var @return = Expression.Constant(2, typeof(int));

// How to combine above expressions and create a function body?
var block = Expression.Block(arguments, code, assign, write, read, @return);
var lambda = Expression.Lambda<Func<string [], int>>(block, new ParameterExpression [] { arguments });

lambda.CompileToMethod(builderMethod); // Error: Variable 'code' of type 'Type: MyDynamicEnum' referenced from scope '', but it is not defined.

The full code is available on this GIST. The error on the last line seems to make sense but I don't know how to fix it. The enumeration MyDynamicEnum is already created as a type but how do I import it into the expression tree context? Any pointers would be appreciated.

回答1:

Solved it by using the correct overload of Expression.Block.

In order to use variables in the expression scope, we have to specify:

Expression.Block(variables.ToArray(), queue);

where variables is an array of ParameterExpression types.