コード例 #1
0
 public override void Emit(IlCompilerContext ctx)
 {
     _target.Emit(ctx);
     _value.Emit(ctx);
     ctx.Il.Emit(OpCodes.Stfld, _field);
     ctx.StackDepth -= 2;
 }
コード例 #2
0
 public override void Emit(IlCompilerContext ctx)
 {
     _expression.Emit(ctx);
     ctx.StackDepth--;
     ctx.Il.Emit(OpCodes.Castclass, _type);
     ctx.StackDepth++;
 }
コード例 #3
0
        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++;
        }
コード例 #4
0
        public override void Emit(IlCompilerContext ctx)
        {
            var ctor = _type.GetConstructor(new Type[] {});

            // ReSharper disable once AssignNullToNotNullAttribute
            ctx.Il.Emit(OpCodes.Newobj, ctor);
            ctx.StackDepth++;
        }
コード例 #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);
        }
コード例 #6
0
 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++;
     }
 }
コード例 #7
0
 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++;
     }
 }
コード例 #8
0
 public override void Emit(IlCompilerContext ctx)
 {
     _value.Emit(ctx);
     ctx.Il.Emit(OpCodes.Stloc, _variable.VariableIndex);
     ctx.StackDepth--;
 }
コード例 #9
0
 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
 }
コード例 #10
0
 public override void Emit(IlCompilerContext ctx)
 {
     ctx.Il.Emit(_value ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
     ctx.StackDepth++;
 }
コード例 #11
0
 public override void Emit(IlCompilerContext ctx)
 {
     _expression.Emit(ctx);
     ctx.Il.Emit(OpCodes.Unbox_Any, _type);
 }
コード例 #12
0
 public override void Emit(IlCompilerContext ctx)
 {
     ctx.Il.Emit(OpCodes.Ldloc, VariableIndex);
     ctx.StackDepth++;
 }
コード例 #13
0
 public abstract void Emit(IlCompilerContext ctx);
コード例 #14
0
 public override void Emit(IlCompilerContext ctx)
 {
     ctx.Il.Emit(OpCodes.Ldarg, ParameterIndex);
     ctx.StackDepth++;
 }