I have fully initialized MethodBuilderand EnumBuilder. MethodBuilderpoints to the entry point of the dynamic assembly. It has the following signature:
public static int Main (string [] args) {...}
The assembly generation code works fine, and I can use Reflection.Emitto verify it. Instead of emitting IL, I want to save the target code from the expression tree. He must:
- declare enum variable
- set it to
- write to the console
- read pause console
- returns enum value as int32
IDENTIFICATION:
var arguments = Expression.Parameter(typeof(string []), "args");
var code = Expression.Variable(builderEnum, "code");
var assign = Expression.Assign(code, Expression.Constant(2, builderEnum));
var write = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type [] { typeof(string) }), Expression.ArrayIndex(arguments, Expression.Constant(0, typeof(int))));
var read = Expression.Call(typeof(Console).GetMethod("ReadKey", new Type [] { typeof(bool) }), Expression.Constant(true, typeof(bool)));
var @return = Expression.Constant(2, typeof(int));
var block = Expression.Block(arguments, code, assign, write, read, @return);
var lambda = Expression.Lambda<Func<string [], int>>(block, new ParameterExpression [] { arguments });
lambda.CompileToMethod(builderMethod);
GIST. , , , , . MyDynamicEnum , ? .