public override void Emit(IlCompilerContext ctx)
 {
     _target.Emit(ctx);
     _value.Emit(ctx);
     ctx.Il.Emit(OpCodes.Stfld, _field);
     ctx.StackDepth -= 2;
 }
 public override void Emit(IlCompilerContext ctx)
 {
     _expression.Emit(ctx);
     ctx.StackDepth--;
     ctx.Il.Emit(OpCodes.Castclass, _type);
     ctx.StackDepth++;
 }
        public override void Emit(IlCompilerContext ctx)
        {
            var field = ctx.SelfType.GetFields(BindingFlagsEx.All)[Index];

            ctx.Il.Emit(OpCodes.Ldarg_0);
            ctx.Il.Emit(OpCodes.Ldfld, field);
            ctx.StackDepth++;
        }
        public override void Emit(IlCompilerContext ctx)
        {
            var ctor = _type.GetConstructor(new Type[] {});

            // ReSharper disable once AssignNullToNotNullAttribute
            ctx.Il.Emit(OpCodes.Newobj, ctor);
            ctx.StackDepth++;
        }
Esempio n. 5
0
        public TDel Compile()
        {
            var delegateType = typeof(TDel);
            var invoke       = delegateType.GetMethod("Invoke");

            var self               = BuildSelf();
            var selfType           = self?.GetType() ?? typeof(object);
            var parametersWithSelf = GetParameterTypesWithSelf(invoke, selfType);
            var returnType         = invoke.ReturnType;
            var method             = new DynamicMethod("foo", MethodAttributes.Public | MethodAttributes.Static,
                                                       CallingConventions.Standard, returnType, parametersWithSelf, typeof(string).Module, true);

            var il      = method.GetILGenerator();
            var context = new IlCompilerContext(il, selfType);

            //declare local variables
            foreach (var variable in Variables)
            {
                il.DeclareLocal(variable.Type());
            }

            //declare "this"
            method.DefineParameter(0, ParameterAttributes.None, "this");

            //declare custom parameters
            foreach (var parameter in Parameters)
            {
                method.DefineParameter(parameter.ParameterIndex, ParameterAttributes.None, parameter.Name);
            }

            //emit il code
            LazyEmits.ForEach(e => e(context));

            //we need to return
            context.Il.Emit(OpCodes.Ret);

            //   Console.WriteLine(context.Il.ToString());

            //if we have a return type, it's OK that there is one item on the stack
            if (returnType != typeof(void))
            {
                context.StackDepth--;
            }

            //if the stack is not aligned, there is some error
            if (context.StackDepth != 0)
            {
                throw new NotSupportedException("Stack error");
            }

            var del = (TDel)(object)method.CreateDelegate(typeof(TDel), self);

            return(del);
        }
 public override void Emit(IlCompilerContext ctx)
 {
     foreach (var arg in _args)
     {
         arg.Emit(ctx);
         ctx.StackDepth--;
     }
     ctx.Il.EmitCall(OpCodes.Call, _method, null);
     if (_method.ReturnType != typeof(void))
     {
         ctx.StackDepth++;
     }
 }
 public override void Emit(IlCompilerContext ctx)
 {
     _target.Emit(ctx);
     ctx.StackDepth--;
     foreach (var arg in _args)
     {
         arg.Emit(ctx);
         ctx.StackDepth--;
     }
     if (_method.IsVirtual)
     {
         ctx.Il.EmitCall(OpCodes.Callvirt, _method, null);
     }
     else
     {
         ctx.Il.EmitCall(OpCodes.Call, _method, null);
     }
     if (_method.ReturnType != typeof(void))
     {
         ctx.StackDepth++;
     }
 }
 public override void Emit(IlCompilerContext ctx)
 {
     _value.Emit(ctx);
     ctx.Il.Emit(OpCodes.Stloc, _variable.VariableIndex);
     ctx.StackDepth--;
 }
 public override void Emit(IlCompilerContext ctx)
 {
     _target.Emit(ctx);
     ctx.Il.Emit(OpCodes.Ldfld, _field);
     //we are still at the same stack size as we consumed the target
 }
 public override void Emit(IlCompilerContext ctx)
 {
     ctx.Il.Emit(_value ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
     ctx.StackDepth++;
 }
 public override void Emit(IlCompilerContext ctx)
 {
     _expression.Emit(ctx);
     ctx.Il.Emit(OpCodes.Unbox_Any, _type);
 }
 public override void Emit(IlCompilerContext ctx)
 {
     ctx.Il.Emit(OpCodes.Ldloc, VariableIndex);
     ctx.StackDepth++;
 }
 public abstract void Emit(IlCompilerContext ctx);
 public override void Emit(IlCompilerContext ctx)
 {
     ctx.Il.Emit(OpCodes.Ldarg, ParameterIndex);
     ctx.StackDepth++;
 }