EmitCall() private method

private EmitCall ( System opcode, System methodInfo, System optionalParameterTypes ) : void
opcode System
methodInfo System
optionalParameterTypes System
return void
コード例 #1
0
 private static void EmitCallMethod(ILGenerator il, MethodInfo methodInfo)
 {
     if (methodInfo.IsStatic)
         il.EmitCall(OpCodes.Call, methodInfo, null);
     else
         il.EmitCall(OpCodes.Callvirt, methodInfo, null);
 }
コード例 #2
0
 /// <summary>
 ///     Generates method invocation code.
 /// </summary>
 /// <param name="il">IL generator to use.</param>
 /// <param name="isStatic">Flag specifying whether method is static.</param>
 /// <param name="isValueType">Flag specifying whether method is on the value type.</param>
 /// <param name="method">Method to invoke.</param>
 protected static void InvokeMethod(ILGenerator il, bool isStatic, bool isValueType, MethodInfo method)
 {
     if (isStatic || isValueType)
     {
         il.EmitCall(OpCodes.Call, method, null);
     }
     else
     {
         il.EmitCall(OpCodes.Callvirt, method, null);
     }
 }
コード例 #3
0
ファイル: Write.cs プロジェクト: bi-tm/openABAP
 public override void BuildAssembly( ILGenerator il )
 {
     System.Type[] types = { typeof(String) };
     if (Format != null && Format.Equals("/")) {
         il.Emit(OpCodes.Ldstr, "\n");
         il.EmitCall(OpCodes.Call, typeof(openABAP.Runtime.Runtime).GetMethod("Write", types), null);
     }
     //push formatted string of output value to stack
     this.Value.PushFormattedString(il);
     il.EmitCall(OpCodes.Call, typeof(openABAP.Runtime.Runtime).GetMethod("Write", types), null);
 }
コード例 #4
0
        public static void GenerateSerializerSwitch(CodeGenContext ctx, ILGenerator il, IDictionary<Type, TypeData> map)
        {
            // arg0: Stream, arg1: object

            var idLocal = il.DeclareLocal(typeof(ushort));

            // get TypeID from object's Type
            var getTypeIDMethod = typeof(Serializer).GetMethod("GetTypeID", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(object) }, null);
            il.Emit(OpCodes.Ldarg_1);
            il.EmitCall(OpCodes.Call, getTypeIDMethod, null);
            il.Emit(OpCodes.Stloc_S, idLocal);

            // write typeID
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldloc_S, idLocal);
            il.EmitCall(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(ushort)), null);

            // +1 for 0 (null)
            var jumpTable = new Label[map.Count + 1];
            jumpTable[0] = il.DefineLabel();
            foreach (var kvp in map)
                jumpTable[kvp.Value.TypeID] = il.DefineLabel();

            il.Emit(OpCodes.Ldloc_S, idLocal);
            il.Emit(OpCodes.Switch, jumpTable);

            ConstructorInfo exceptionCtor = typeof(Exception).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);
            il.Emit(OpCodes.Newobj, exceptionCtor);
            il.Emit(OpCodes.Throw);

            /* null case */
            il.MarkLabel(jumpTable[0]);
            il.Emit(OpCodes.Ret);

            /* cases for types */
            foreach (var kvp in map)
            {
                var type = kvp.Key;
                var data = kvp.Value;

                il.MarkLabel(jumpTable[data.TypeID]);

                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);

                il.EmitCall(OpCodes.Call, data.WriterMethodInfo, null);

                il.Emit(OpCodes.Ret);
            }
        }
コード例 #5
0
ファイル: DelegateConversion.cs プロジェクト: GISwilson/Cyjb
 /// <summary>
 /// 写入类型转换的指令。
 /// </summary>
 /// <param name="generator">IL 的指令生成器。</param>
 /// <param name="inputType">要转换的对象的类型。</param>
 /// <param name="outputType">要将输入对象转换到的类型。</param>
 /// <param name="isChecked">是否执行溢出检查。</param>
 public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
 {
     MethodInfo method = converter.Method;
     if (method.IsStatic && method.GetParametersNoCopy().Length == 1)
     {
         // 没有闭包的静态方法可以直接调用。
         generator.EmitCall(method);
     }
     else
     {
         // 有闭包的静态方法,或实例方法,其参数不能直接写入 IL,因此直接调用 Convert 的 ChangeType 方法。
         generator.EmitCall(changeType.MakeGenericMethod(inputType, outputType));
     }
 }
コード例 #6
0
ファイル: TypeAccessor.cs プロジェクト: VictorTomaili/Sanity
        private static void WriteGetter(ILGenerator il, Type type, PropertyInfo[] props, FieldInfo[] fields, bool isStatic)
        {
            var loc = type.IsValueType ? il.DeclareLocal(type) : null;
            OpCode propName = isStatic ? OpCodes.Ldarg_1 : OpCodes.Ldarg_2, target = isStatic ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1;
            foreach (var prop in props)
            {
                MethodInfo getter;
                if (prop.GetIndexParameters().Length != 0 || !prop.CanRead || (getter = prop.GetGetMethod(false)) == null) continue;

                var next = il.DefineLabel();
                il.Emit(propName);
                il.Emit(OpCodes.Ldstr, prop.Name);
                il.EmitCall(OpCodes.Call, strinqEquals, null);
                il.Emit(OpCodes.Brfalse_S, next);
                // match:
                il.Emit(target);
                Cast(il, type, loc);
                il.EmitCall(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, getter, null);
                if (prop.PropertyType.IsValueType)
                {
                    il.Emit(OpCodes.Box, prop.PropertyType);
                }
                il.Emit(OpCodes.Ret);
                // not match:
                il.MarkLabel(next);
            }
            foreach (var field in fields)
            {
                var next = il.DefineLabel();
                il.Emit(propName);
                il.Emit(OpCodes.Ldstr, field.Name);
                il.EmitCall(OpCodes.Call, strinqEquals, null);
                il.Emit(OpCodes.Brfalse_S, next);
                // match:
                il.Emit(target);
                Cast(il, type, loc);
                il.Emit(OpCodes.Ldfld, field);
                if (field.FieldType.IsValueType)
                {
                    il.Emit(OpCodes.Box, field.FieldType);
                }
                il.Emit(OpCodes.Ret);
                // not match:
                il.MarkLabel(next);
            }
            il.Emit(OpCodes.Ldstr, "name");
            il.Emit(OpCodes.Newobj, typeof(ArgumentOutOfRangeException).GetConstructor(new Type[] { typeof(string) }));
            il.Emit(OpCodes.Throw);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="il"></param>
        /// <param name="paramAttr"></param>
        /// <param name="parameterType"></param>
        public override void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType)
        {
            ProviderDependencyAttribute attr = (ProviderDependencyAttribute) paramAttr;
            MethodInfo getHeadOfChain = GetPropertyGetter<IBuilderContext>("HeadOfChain", typeof (IBuilderStrategy));
            MethodInfo buildUp = GetMethodInfo<IBuilderStrategy>("BuildUp",
                                                                 typeof (IBuilderContext), typeof (Type), typeof (object),
                                                                 typeof (string));

            PropertyInfo prop =
                attr.ProviderHostType.GetProperty(attr.ProviderGetterProperty, BindingFlags.Static | BindingFlags.Public);
            if (prop == null)
            {
                throw new ArgumentException();
            }

            MethodInfo propInvoker = prop.GetGetMethod();
            if (propInvoker == null)
            {
                throw new ArgumentException();
            }
            Guid.NewGuid();
            MethodInfo newGuidMethod = typeof (Guid).GetMethod("NewGuid");
            MethodInfo guidToStringMethod = typeof (Guid).GetMethod("ToString", new Type[] {});
            if ((newGuidMethod == null) || (guidToStringMethod == null))
            {
                throw new ArgumentException();
            }

            //object value (declaration)
            LocalBuilder valueIndex = il.DeclareLocal(typeof (object));

            //object value = prop.GetGetMethod().Invoke(attr.ProviderHostType, null);
            //value = propInvoker.Invoke(null) (return value remains in the stack)
            il.EmitCall(OpCodes.Call, propInvoker, null);
            il.Emit(OpCodes.Stloc, valueIndex);

            //string id = Guid.NewGuid().ToString();
            //il.Emit(OpCodes.Ldtoken, typeof(Guid));
            //il.EmitCall(OpCodes.Call, newGuidMethod, null);
            //il.EmitCall(OpCodes.Call, guidToStringMethod, null);
            //il.Emit(OpCodes.Stloc, idIndex);

            // Get the head of the context chain
            il.Emit(OpCodes.Ldarg_0); // Get context onto the stack
            il.EmitCall(OpCodes.Callvirt, getHeadOfChain, null); // Now head of chain is on the stack

            // Build up parameters to the BuildUp call - context, type, existing, id
            il.Emit(OpCodes.Ldarg_0); // Push context onto stack
            EmitLoadType(il, parameterType);

            // Existing object is value
            il.Emit(OpCodes.Ldloc, valueIndex);

            // And the id
            //il.Emit(OpCodes.Ldloc,idIndex);
            il.Emit(OpCodes.Ldarg_3);

            // Call buildup on head of the chain
            il.EmitCall(OpCodes.Callvirt, buildUp, null);
        }
コード例 #8
0
ファイル: In.cs プロジェクト: paf31/BF
 public void EmitIL(ILGenerator body, FieldInfo tape, FieldInfo ptr)
 {
     body.Emit(OpCodes.Ldsfld, tape);
     body.Emit(OpCodes.Ldsfld, ptr);
     body.EmitCall(OpCodes.Call,
         typeof(Console).GetMethod("Read", new Type[0]), null);
     body.Emit(OpCodes.Stelem_I4);
 }
コード例 #9
0
        /// <summary>
        /// Implements code for end invocations.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="interfaceType"></param>
        /// <param name="generator"></param>
        /// <param name="interfaceMethod"></param>
        protected override void OnInvokeEnd(TypeBuilder type, Type interfaceType, ILGenerator generator, MethodInfo interfaceMethod)
        {
            var skip = interfaceMethod.GetCustomAttributes(typeof(SkipProbeAttribute), false).OfType<SkipProbeAttribute>().FirstOrDefault();
            if ( skip != null && (skip.SkipActions & ProbeActions.End) == ProbeActions.End)
                return;

            var probeType = typeof(IMethodCallProbe<>).MakeGenericType(interfaceType);
            var endInvoke = probeType.GetMethod("OnEndInvoke");
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldfld, probeField());
            generator.Emit(OpCodes.Ldtoken, interfaceMethod);
            generator.EmitCall(OpCodes.Call, typeof(MethodBase).GetMethod("GetMethodFromHandle", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(RuntimeMethodHandle) }, null), null);
            generator.Emit(OpCodes.Ldarg_0);
            generator.EmitCall(OpCodes.Callvirt, endInvoke, null);

            base.OnInvokeEnd(type, interfaceType, generator, interfaceMethod);
        }
コード例 #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="il"></param>
        /// <param name="paramAttr"></param>
        /// <param name="parameterType"></param>
        public override void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType)
        {
            ServiceDependencyAttribute attr = (ServiceDependencyAttribute) paramAttr;
            MethodInfo getLocator = GetPropertyGetter<IBuilderContext>("Locator", typeof (IReadWriteLocator));
            MethodInfo getFromLocator = ObtainGetFromLocatorMethod();
            MethodInfo getServices =
                GetPropertyGetter<CompositionContainer>("Services", typeof (IServiceCollection));
            MethodInfo getFromServices = GetMethodInfo<IServiceCollection>("Get", typeof (Type), typeof (bool));
            ConstructorInfo newDependencyKey =
                GetConstructor<DependencyResolutionLocatorKey>(typeof (Type), typeof (string));

            // context.get_Locator
            il.Emit(OpCodes.Ldarg_0);
            il.EmitCall(OpCodes.Callvirt, getLocator, null);

            // new DependencyResolutionContainer(typeof(CompositionContainer), null)
            EmitLoadType(il, typeof (CompositionContainer));
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Newobj, newDependencyKey);
            // locator.Get(key)
            il.EmitCall(OpCodes.Callvirt, getFromLocator, null);
            il.Emit(OpCodes.Castclass, typeof (CompositionContainer));

            // container.get_Services
            il.EmitCall(OpCodes.Callvirt, getServices, null);

            if (attr.Type != null)
            {
                EmitLoadType(il, attr.Type);
            }
            else
            {
                EmitLoadType(il, parameterType);
            }

            if (attr.Required)
            {
                il.Emit(OpCodes.Ldc_I4_1);
            }
            else
            {
                il.Emit(OpCodes.Ldc_I4_0);
            }
            il.EmitCall(OpCodes.Callvirt, getFromServices, null);
        }
コード例 #11
0
ファイル: Compute.cs プロジェクト: bi-tm/openABAP
 public override void BuildAssembly( ILGenerator il )
 {
     System.Type[] types = {typeof(openABAP.Runtime.IfValue)};
     System.Type t = this.Target.GetRuntimeType();
     MethodInfo mi = t.GetMethod("Set", types);
     this.Target.PushValue( il );
     this.Expression.PushValue( il );
     il.EmitCall (OpCodes.Callvirt, mi, null);
 }
コード例 #12
0
ファイル: Expression.cs プロジェクト: bi-tm/openABAP
 public void PushValue(ILGenerator il)
 {
     System.Type[] types = { typeof(string), typeof(openABAP.Runtime.IfValue) };
     MethodInfo mi = typeof(openABAP.Runtime.IfValue).GetMethod("Calculate", types);
     this.LeftChild.PushValue(il);
     il.Emit(OpCodes.Ldstr, this.Operator);
     this.RightChild.PushValue(il);
     il.EmitCall(OpCodes.Callvirt, mi, null);
 }
コード例 #13
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, System.Reflection.Emit.ILGenerator il)
        {
            var convention = config.Conventions.OfType <TransactionProxyConvention>().First();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.EmitCall(OpCodes.Callvirt, convention.GetValueEntiresTypedMethod.Method, null);
            il.Emit(OpCodes.Ret);
        }
コード例 #14
0
ファイル: Out.cs プロジェクト: paf31/BF
 public void EmitIL(ILGenerator body, FieldInfo tape, FieldInfo ptr)
 {
     body.Emit(OpCodes.Ldsfld, tape);
     body.Emit(OpCodes.Ldsfld, ptr);
     body.Emit(OpCodes.Ldelem_I4);
     body.Emit(OpCodes.Conv_U2);
     body.EmitCall(OpCodes.Call,
         typeof(Console).GetMethod("Write", new Type[] { typeof(char) }), null);
 }
コード例 #15
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, System.Reflection.Emit.ILGenerator il)
        {
            var convention = config.Conventions.OfType <TransactionProxyConvention>().First();

            var method = typeof(ITransactionProxy <>).MakeGenericType(convention.ItemType).GetMethod("SetTargets");

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Castclass, typeof(IEnumerable <>).MakeGenericType(convention.ItemType));
            il.EmitCall(OpCodes.Callvirt, method, null);
            il.Emit(OpCodes.Ret);
        }
コード例 #16
0
ファイル: EmitUtils.cs プロジェクト: Oman/Maleos
        public static void Call(ILGenerator gen, MethodInfo method)
        {
            if (gen == null)
            {
                throw new ArgumentNullException("gen");
            }

            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            if (method.IsFinal || !method.IsVirtual)
            {
                gen.EmitCall(OpCodes.Call, method, null);
            }
            else
            {
                gen.EmitCall(OpCodes.Callvirt, method, null);
            }
        }
コード例 #17
0
 /// <summary>
 /// 写入类型转换的 IL 指令。
 /// </summary>
 /// <param name="generator">IL 的指令生成器。</param>
 /// <param name="inputType">要转换的对象的类型。</param>
 /// <param name="outputType">要将输入对象转换到的类型。</param>
 /// <param name="isChecked">是否执行溢出检查。</param>
 public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
 {
     Contract.Assume(inputType.IsNullable());
     Type inputUnderlyingType = Nullable.GetUnderlyingType(inputType);
     generator.EmitCall(inputType.GetMethod("get_Value"));
     if (inputUnderlyingType != outputType)
     {
         Conversion conversion = ConversionFactory.GetConversion(inputUnderlyingType, outputType);
         Contract.Assume(conversion != null);
         conversion.Emit(generator, inputUnderlyingType, outputType, isChecked);
     }
 }
コード例 #18
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, System.Reflection.Emit.ILGenerator il)
        {
            var convention = config.Conventions.OfType <TransactionProxyConvention>().First();

            var listCtor = convention.TargetsField.MemberType.GetConstructor(new[] { typeof(IEnumerable <>).MakeGenericType(convention.ItemType) });

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Newobj, listCtor);
            il.Emit(OpCodes.Stfld, convention.TargetsField.Field);
            il.Emit(OpCodes.Ldarg_0);
            il.EmitCall(OpCodes.Callvirt, typeof(ICommitable).GetMethod("Rollback"), null);
            il.Emit(OpCodes.Ret);
        }
コード例 #19
0
ファイル: EmitHelper.cs プロジェクト: Gilgamash/Chloe
 public static void SetValueIL(ILGenerator il, MemberInfo member)
 {
     MemberTypes memberType = member.MemberType;
     if (memberType == MemberTypes.Property)
     {
         MethodInfo setter = ((PropertyInfo)member).GetSetMethod();
         il.EmitCall(OpCodes.Callvirt, setter, null);//给属性赋值
     }
     else if (memberType == MemberTypes.Field)
     {
         il.Emit(OpCodes.Stfld, ((FieldInfo)member));//给字段赋值
     }
     else
         throw new NotSupportedException();
 }
コード例 #20
0
ファイル: DynamicBase.cs プロジェクト: badamczewski/CSpec
        /// <summary>
        /// Calls the injected method.
        /// </summary>
        /// <param name="ilGenerator">The il generator.</param>
        /// <param name="method">The method.</param>
        /// <param name="hasExternalParameters"></param>
        protected static void CallInjectedMethod(ILGenerator ilGenerator, MethodInfo method, bool hasExternalParameters)
        {
            if (!method.IsStatic)
            {
                ilGenerator.Emit(OpCodes.Ldarg_0);
            }

            ilGenerator.Emit(OpCodes.Ldarg_1);

            if (hasExternalParameters)
            {
                ilGenerator.Emit(OpCodes.Ldloc_0);
            }

            ilGenerator.EmitCall(OpCodes.Call, method, null);
        }
コード例 #21
0
ファイル: CodeEquals.cs プロジェクト: nickchal/pash
		public override void Generate (ILGenerator gen)
		{
			if (t1.IsPrimitive)
			{
				exp1.Generate (gen);
				exp2.Generate (gen);
				gen.Emit (OpCodes.Ceq);
			}
			else
			{
				exp1.Generate (gen);
				exp2.Generate (gen);
//				gen.Emit (OpCodes.Ceq);
				gen.EmitCall (OpCodes.Callvirt, t1.GetMethod ("Equals", new Type[] {t2}), null);
			}
		}
コード例 #22
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, System.Reflection.Emit.ILGenerator il)
        {
            var convention            = config.Conventions.OfType <TransactionProxyConvention>().First();
            var stringEqual           = typeof(string).GetMethod("op_Equality", new Type[] { typeof(string), typeof(string) });
            var getValueEntriesMethod = typeof(TransactionProxyHelper).GetMethod("GetValueEntries");

            var propertyLabels = new Dictionary <TransactionProxyConvention.TransactionProxyProperty, Label>();
            var returnLabel    = il.DefineLabel();
            var notFoundLabel  = il.DefineLabel();

            foreach (var item in convention.TransactionProxyProperties)
            {
                var label = il.DefineLabel();
                propertyLabels.Add(item, label);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldstr, item.Property.Name);
                il.Emit(OpCodes.Call, stringEqual);
                il.Emit(OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Ceq);
                il.Emit(OpCodes.Brfalse, label);
                il.Emit(OpCodes.Nop);
            }

            il.Emit(OpCodes.Br, notFoundLabel);

            foreach (var item in propertyLabels)
            {
                var method = getValueEntriesMethod.MakeGenericMethod(item.Key.Property.PropertyType, convention.ItemType);
                il.MarkLabel(item.Value);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, item.Key.ValuesProperty.BackingField);
                il.EmitCall(OpCodes.Call, method, null);
                il.Emit(OpCodes.Br, returnLabel);
            }

            var exceptionType = typeof(ArgumentOutOfRangeException);

            il.MarkLabel(notFoundLabel);

            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Newobj, exceptionType.GetConstructor(new Type[] { typeof(string) }));
            il.ThrowException(exceptionType);

            il.MarkLabel(returnLabel);
            il.Emit(OpCodes.Ret);
        }
コード例 #23
0
ファイル: WriteOp.cs プロジェクト: dreasgrech/yabfcompiler
        /// <summary>
        /// Given an offset of 2, generates:
        /// Console.Write((char) buffer[index + 2]);
        /// 
        /// TODO: This method is missing the Offset usage
        /// </summary>
        /// <param name="ilg"></param>
        /// <param name="array"></param>
        /// <param name="ptr"></param>
        public void Emit(ILGenerator ilg, LocalBuilder array, LocalBuilder ptr)
        {
            for (int i = 0; i < Repeated; i++)
            {
                ilg.Emit(OpCodes.Ldloc, array);
                if (Constant != null)
                {
                    ILGeneratorHelpers.Load32BitIntegerConstant(ilg, Constant.Value);
                }
                else
                {
                    ilg.Emit(OpCodes.Ldloc, ptr);
                }

                ilg.Emit(OpCodes.Ldelem_U1);
                ilg.EmitCall(OpCodes.Call, consoleWriteMethodInfo, null);
            }
        }
コード例 #24
0
 /// <summary>
 /// 写入类型转换的 IL 指令。
 /// </summary>
 /// <param name="generator">IL 的指令生成器。</param>
 /// <param name="inputType">要被转换的类型。</param>
 /// <param name="outputType">要转换到的类型。</param>
 /// <param name="isChecked">是否执行溢出检查。</param>
 public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
 {
     Contract.Assume(inputType.IsNullable());
     Type inputUnderlyingType = Nullable.GetUnderlyingType(inputType);
     Type outputUnderlyingType = Nullable.GetUnderlyingType(outputType);
     // 定义变量和标签
     LocalBuilder inputLocal = generator.GetLocal(inputType);
     Label trueCase = generator.DefineLabel();
     Label endConvert = generator.DefineLabel();
     // inputLocal = value;
     generator.Emit(OpCodes.Stloc, inputLocal);
     // if (input.HasValue)
     generator.Emit(OpCodes.Ldloca, inputLocal);
     generator.EmitCall(inputType.GetMethod("get_HasValue"));
     generator.Emit(OpCodes.Brtrue, trueCase);
     // return null
     if (outputUnderlyingType != null)
     {
         // Nullable<T>。
         generator.EmitDefault(outputType);
     }
     else
     {
         // 引用类型。
         generator.Emit(OpCodes.Ldnull);
     }
     generator.Emit(OpCodes.Br, endConvert);
     // else
     generator.MarkLabel(trueCase);
     // (outputType)input.GetValueOrDefault();
     generator.Emit(OpCodes.Ldloca, inputLocal);
     generator.FreeLocal(inputLocal);
     generator.EmitCall(inputType.GetMethod("GetValueOrDefault", Type.EmptyTypes));
     ConversionFactory.GetConversion(inputUnderlyingType, outputUnderlyingType ?? outputType)
         .Emit(generator, inputUnderlyingType, outputUnderlyingType ?? outputType, isChecked);
     if (outputUnderlyingType != null)
     {
         ConstructorInfo ctor = outputType.GetConstructor(new[] { outputUnderlyingType });
         Contract.Assume(ctor != null);
         generator.Emit(OpCodes.Newobj, ctor);
     }
     generator.MarkLabel(endConvert);
 }
コード例 #25
0
ファイル: Helpers.cs プロジェクト: ppdai/TripSerializer.Net
		public static void GenDeserializerCall(CodeGenContext ctx, ILGenerator il, Type type)
		{
			// We can call the Deserializer method directly for:
			// - Value types
			// - Array types
			// - Sealed types with static Deserializer method, as the method will handle null
			// Other reference types go through the DeserializesSwitch

			bool direct;

			if (type.IsValueType || type.IsArray)
				direct = true;
			else if (type.IsSealed && ctx.IsGenerated(type) == false)
				direct = true;
			else
				direct = false;

			var method = direct ? ctx.GetReaderMethodInfo(type) : ctx.DeserializerSwitchMethodInfo;

			il.EmitCall(OpCodes.Call, method, null);
		}
コード例 #26
0
        static void GenSerializerBody(CodeGenContext ctx, Type type, ILGenerator il)
        {
            if (type.IsClass)
            {
                Type objStackType = typeof(ObjectList);
                MethodInfo getAddMethod = objStackType.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(object) }, null);

                var endLabel = il.DefineLabel();

                //==if(objList==null)  goto endLabel;
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Brfalse_S, endLabel);

                //== objList.Add(value);
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldarg_1);
                il.EmitCall(OpCodes.Call, getAddMethod, null);

                il.MarkLabel(endLabel);
            }

            var fields = Helpers.GetFieldInfos(type);

            foreach (var field in fields)
            {
                // Note: the user defined value type is not passed as reference. could cause perf problems with big structs

                il.Emit(OpCodes.Ldarg_0);
                if (type.IsValueType)
                    il.Emit(OpCodes.Ldarga_S, 1);
                else
                    il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldfld, field);
                il.Emit(OpCodes.Ldarg_2);

                GenSerializerCall(ctx, il, field.FieldType);
            }

            il.Emit(OpCodes.Ret);
        }
コード例 #27
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="typeToBuild"></param>
        /// <param name="existing"></param>
        /// <param name="idToBuild"></param>
        /// <param name="il"></param>
        protected override void BuildUp(
			IBuilderContext context, Type typeToBuild, object existing, string idToBuild, ILGenerator il)
        {
            List<PropertyInfo> properties = new List<PropertyInfo>(GetInjectionProperties(context, typeToBuild, idToBuild));
            if (properties.Count > 0)
            {
                // At entry, object we're building up is on top of the stack.
                // We'll need that a lot, so create a local to store it, and
                // put it in there
                LocalBuilder existingObject = il.DeclareLocal(typeToBuild);
                il.Emit(OpCodes.Stloc, existingObject);

                foreach (PropertyInfo prop in properties)
                {
                    il.Emit(OpCodes.Ldloc, existingObject);
                    EmitResolveProperty(il, prop);
                    il.EmitCall(OpCodes.Callvirt, prop.GetSetMethod(), null);
                }

                // On exit we need to make sure the existing object is top of stack again
                il.Emit(OpCodes.Ldloc, existingObject);
            }
        }
コード例 #28
0
ファイル: DecimalConversion.cs プロジェクト: GISwilson/Cyjb
 /// <summary>
 /// 写入类型转换的 IL 指令。
 /// </summary>
 /// <param name="generator">IL 的指令生成器。</param>
 /// <param name="inputType">要转换的对象的类型。</param>
 /// <param name="outputType">要将输入对象转换到的类型。</param>
 /// <param name="isChecked">是否执行溢出检查。</param>
 public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
 {
     Contract.Assume((inputType == typeof(decimal) && outputType.IsNumeric()) ||
         (outputType == typeof(decimal) && inputType.IsNumeric()));
     MethodInfo method;
     if (inputType == typeof(decimal))
     {
         if (outputType.IsEnum)
         {
             outputType = Enum.GetUnderlyingType(outputType);
         }
         method = UserConversionCache.GetConversionTo(inputType, outputType);
     }
     else
     {
         if (inputType.IsEnum)
         {
             inputType = Enum.GetUnderlyingType(inputType);
         }
         method = UserConversionCache.GetConversionFrom(outputType, inputType);
     }
     generator.EmitCall(method);
 }
コード例 #29
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="typeToBuild"></param>
        /// <param name="existing"></param>
        /// <param name="idToBuild"></param>
        /// <param name="il"></param>
        protected override void BuildUp(
			IBuilderContext context, Type typeToBuild, object existing, string idToBuild, ILGenerator il)
        {
            IMethodChooserPolicy chooser = GetChooserFromContext(context, typeToBuild, idToBuild);
            List<MethodInfo> methods = new List<MethodInfo>(chooser.GetMethods(typeToBuild));

            if (methods.Count > 0)
            {
                LocalBuilder objectToBuild = il.DeclareLocal(typeToBuild);
                il.Emit(OpCodes.Stloc, objectToBuild);

                foreach (MethodInfo method in chooser.GetMethods(typeToBuild))
                {
                    il.Emit(OpCodes.Ldloc, objectToBuild);
                    foreach (ParameterInfo paramInfo in method.GetParameters())
                    {
                        EmitResolveParameter(il, paramInfo);
                    }
                    il.EmitCall(OpCodes.Call, method, null);
                }
                il.Emit(OpCodes.Ldloc, objectToBuild);
            }
        }
コード例 #30
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, System.Reflection.Emit.ILGenerator il)
        {
            var convention               = config.Conventions.OfType <TransactionProxyConvention>().First();
            var addValueMethod           = typeof(TransactionProxyHelper).GetMethod("AddValue", BindingFlags.Static | BindingFlags.Public);
            var getValueMethod           = typeof(TransactionProxyHelper).GetMethod("GetValue", BindingFlags.Static | BindingFlags.Public);
            var setCollectionValueMethod = typeof(TransactionProxyHelper).GetMethod("SetCollectionValue", BindingFlags.Static | BindingFlags.Public);


            var listType           = typeof(IEnumerable <>).MakeGenericType(config.BaseType);
            var enumeratorType     = typeof(IEnumerator <>).MakeGenericType(config.BaseType);
            var enumeratorVariable = il.DeclareLocal(enumeratorType);
            var currentVariable    = il.DeclareLocal(config.BaseType);

            foreach (var item in convention.TransactionProxyProperties)
            {
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Newobj, item.ValuesProperty.MemberType.GetConstructor(new Type[] { }));
                il.Emit(OpCodes.Stfld, item.ValuesProperty.BackingField);
            }


            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, convention.TargetsField.Field);
            il.EmitCall(OpCodes.Callvirt, listType.GetMethod("GetEnumerator"), null);
            il.Emit(OpCodes.Stloc, enumeratorVariable);

            var loopLabel = il.DefineLabel();
            var nextLabel = il.DefineLabel();

            var tryBlock = il.BeginExceptionBlock();

            il.MarkLabel(loopLabel);
            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.EmitCall(OpCodes.Callvirt, typeof(IEnumerator).GetMethod("MoveNext"), null);
            il.Emit(OpCodes.Brfalse, nextLabel);

            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.EmitCall(OpCodes.Callvirt, enumeratorType.GetProperty("Current").GetGetMethod(), null);
            il.Emit(OpCodes.Stloc, currentVariable);

            foreach (var item in convention.TransactionProxyProperties)
            {
                il.Emit(OpCodes.Ldloc, currentVariable);
                il.EmitCall(OpCodes.Callvirt, item.Property.GetGetMethod(true), null);
                il.Emit(OpCodes.Ldloc, currentVariable);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, item.ValuesProperty.BackingField);
                il.EmitCall(OpCodes.Call, addValueMethod.MakeGenericMethod(item.Property.PropertyType, config.BaseType), null);
            }

            il.Emit(OpCodes.Br, loopLabel);

            var endFinally = il.DefineLabel();

            il.BeginFinallyBlock();
            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Ceq);
            il.Emit(OpCodes.Brtrue_S, endFinally);
            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.EmitCall(OpCodes.Callvirt, typeof(IDisposable).GetMethod("Dispose"), null);
            il.MarkLabel(endFinally);
            il.EndExceptionBlock();

            il.MarkLabel(nextLabel);

            foreach (var item in convention.TransactionProxyProperties)
            {
                Type itemType;
                if (IsCollectionType(item.Property.PropertyType, out itemType))
                {
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldfld, item.ValuesProperty.BackingField);
                    il.Emit(OpCodes.Ldarg_0);
                    il.EmitCall(OpCodes.Callvirt, item.Property.GetGetMethod(true), null);
                    il.EmitCall(OpCodes.Call, setCollectionValueMethod.MakeGenericMethod(itemType, item.Property.PropertyType, config.BaseType), null);
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldfld, item.ValuesProperty.BackingField);
                    il.EmitCall(OpCodes.Call, getValueMethod.MakeGenericMethod(item.Property.PropertyType, config.BaseType), null);
                    il.EmitCall(OpCodes.Callvirt, item.Property.GetSetMethod(true), null);
                }

                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, item.ValuesProperty.BackingField);
                il.EmitCall(OpCodes.Callvirt, item.ValuesProperty.BackingField.FieldType.GetProperty("Count").GetGetMethod(), null);
                il.Emit(OpCodes.Ldc_I4_1);
                il.Emit(OpCodes.Ceq);
                il.Emit(OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Ceq);
                il.EmitCall(OpCodes.Callvirt, item.HasMultipleValuesProperty.PropertySetMethod, null);
            }

            il.Emit(OpCodes.Ret);
        }
コード例 #31
0
ファイル: Field.cs プロジェクト: bi-tm/openABAP
 public void PushFormattedString( ILGenerator il )
 {
     this.PushValue( il );
     il.EmitCall (OpCodes.Callvirt, fi.FieldType.GetMethod("OutputString"), null);
 }
コード例 #32
0
        private static void WriteSetter(ILGenerator il, Type type, PropertyInfo[] props, FieldInfo[] fields, bool isStatic)
        {
            if (type.IsValueType)
            {
                il.Emit(OpCodes.Ldstr, "Write is not supported for structs");
                il.Emit(OpCodes.Newobj, typeof(NotSupportedException).GetConstructor(new Type[] { typeof(string) }));
                il.Emit(OpCodes.Throw);
            }
            else
            {
                OpCode propName = isStatic ? OpCodes.Ldarg_1 : OpCodes.Ldarg_2,
                       target = isStatic ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1,
                       value = isStatic ? OpCodes.Ldarg_2 : OpCodes.Ldarg_3;
                LocalBuilder loc = type.IsValueType ? il.DeclareLocal(type) : null;
                foreach (PropertyInfo prop in props)
                {
                    if (prop.GetIndexParameters().Length != 0 || !prop.CanWrite) continue;

                    Label next = il.DefineLabel();
                    il.Emit(propName);
                    il.Emit(OpCodes.Ldstr, prop.Name);
                    il.EmitCall(OpCodes.Call, strinqEquals, null);
                    il.Emit(OpCodes.Brfalse_S, next);
                    // match:
                    il.Emit(target);
                    Cast(il, type, loc);
                    il.Emit(value);
                    Cast(il, prop.PropertyType, null);
                    il.EmitCall(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, prop.GetSetMethod(), null);
                    il.Emit(OpCodes.Ret);
                    // not match:
                    il.MarkLabel(next);
                }
                foreach (FieldInfo field in fields)
                {
                    Label next = il.DefineLabel();
                    il.Emit(propName);
                    il.Emit(OpCodes.Ldstr, field.Name);
                    il.EmitCall(OpCodes.Call, strinqEquals, null);
                    il.Emit(OpCodes.Brfalse_S, next);
                    // match:
                    il.Emit(target);
                    Cast(il, type, loc);
                    il.Emit(value);
                    Cast(il, field.FieldType, null);
                    il.Emit(OpCodes.Stfld, field);
                    il.Emit(OpCodes.Ret);
                    // not match:
                    il.MarkLabel(next);
                }
                il.Emit(OpCodes.Ldstr, "name");
                il.Emit(OpCodes.Newobj, typeof(ArgumentOutOfRangeException).GetConstructor(new Type[] { typeof(string) }));
                il.Emit(OpCodes.Throw);
            }
        }
コード例 #33
0
 public override void Save(ILGenerator Generator)
 {
     Generator.EmitCall(SetMethod.Builder.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, SetMethod.Builder, null);
 }
コード例 #34
0
        protected override void GenerateMethod(ILGenerator il, System.Reflection.MethodInfo method, System.Reflection.MethodInfo interfaceMethod)
        {
            // In Parameters
            ArrayList inParams = new ArrayList();
            // Ref or Out Parameters
            ArrayList refOutParams = new ArrayList();

            foreach (ParameterInfo paramInfo in interfaceMethod.GetParameters())
            {
                if (paramInfo.IsRetval || paramInfo.IsOut)
                {
                    refOutParams.Add(paramInfo);
                }
                else
                {
                    inParams.Add(paramInfo);
                }
            }

            proxyGenerator.PushTarget(il);

            //LocalBuilder methodToCall = il.DeclareLocal(typeof(MethodInfo));
            //il.Emit(OpCodes.Newobj, interfaceMethod);
            //il.Emit(OpCodes.Ldloc, methodToCall);
            il.Emit(OpCodes.Ldstr, interfaceMethod.Name);

            // Parameter #2
            LocalBuilder parameters = il.DeclareLocal(typeof(Object[]));
            il.Emit(OpCodes.Ldc_I4, inParams.Count);
            il.Emit(OpCodes.Newarr, typeof(Object));
            il.Emit(OpCodes.Stloc, parameters);

            int paramIndex = 0;
            foreach (ParameterInfo paramInfo in inParams)
            {
                il.Emit(OpCodes.Ldloc, parameters);
                il.Emit(OpCodes.Ldc_I4, paramIndex);
                il.Emit(OpCodes.Ldarg, paramInfo.Position + 1);
                if (paramInfo.ParameterType.IsValueType)
                {
                    il.Emit(OpCodes.Box, paramInfo.ParameterType);
                }
                il.Emit(OpCodes.Stelem_Ref);

                paramIndex++;
            }

            il.Emit(OpCodes.Ldloc, parameters);

            // Call Invoke method and save result
            LocalBuilder results = il.DeclareLocal(typeof(Object[]));
            il.EmitCall(OpCodes.Callvirt, InvokeMethod, null);
            il.Emit(OpCodes.Stloc, results);

            int resultIndex = (interfaceMethod.ReturnType == typeof(void) ? 0 : 1);
            foreach (ParameterInfo paramInfo in refOutParams)
            {
                il.Emit(OpCodes.Ldarg, paramInfo.Position + 1);
                il.Emit(OpCodes.Ldloc, results);

                // Cast / Unbox the return value
                il.Emit(OpCodes.Ldc_I4, resultIndex);
                il.Emit(OpCodes.Ldelem_Ref);

                Type elementType = paramInfo.ParameterType.GetElementType();
                if (elementType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox, elementType);
                    il.Emit(OpCodes.Ldobj, elementType);
                    il.Emit(OpCodes.Stobj, elementType);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, elementType);
                    il.Emit(OpCodes.Stind_Ref);
                }

                resultIndex++;
            }

            if (interfaceMethod.ReturnType != typeof(void))
            {
                il.Emit(OpCodes.Ldloc, results);

                // Cast / Unbox the return value
                il.Emit(OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Ldelem_Ref);
                if (interfaceMethod.ReturnType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox, interfaceMethod.ReturnType);
                    il.Emit(OpCodes.Ldobj, interfaceMethod.ReturnType);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, interfaceMethod.ReturnType);
                }
            }
        }
コード例 #35
0
ファイル: DeserializerStub.cs プロジェクト: pczy/Pub.Class
        static void GenDeserializerBodyForArray(CodeGenContext ctx, Type type, ILGenerator il) {
            var elemType = type.GetElementType();

            var lenLocal = il.DeclareLocal(typeof(uint));

            // read array len
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldloca_S, lenLocal);
            il.EmitCall(OpCodes.Call, ctx.GetReaderMethodInfo(typeof(uint)), null);

            var notNullLabel = il.DefineLabel();

            /* if len == 0, return null */
            il.Emit(OpCodes.Ldloc_S, lenLocal);
            il.Emit(OpCodes.Brtrue_S, notNullLabel);

            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Stind_Ref);
            il.Emit(OpCodes.Ret);

            il.MarkLabel(notNullLabel);

            var arrLocal = il.DeclareLocal(type);

            // create new array with len - 1
            il.Emit(OpCodes.Ldloc_S, lenLocal);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Sub);
            il.Emit(OpCodes.Newarr, elemType);
            il.Emit(OpCodes.Stloc_S, arrLocal);

            // declare i
            var idxLocal = il.DeclareLocal(typeof(int));

            // i = 0
            il.Emit(OpCodes.Ldc_I4_0);
            il.Emit(OpCodes.Stloc_S, idxLocal);

            var loopBodyLabel = il.DefineLabel();
            var loopCheckLabel = il.DefineLabel();

            il.Emit(OpCodes.Br_S, loopCheckLabel);

            // loop body
            il.MarkLabel(loopBodyLabel);

            // read element to arr[i]
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldloc_S, arrLocal);
            il.Emit(OpCodes.Ldloc_S, idxLocal);
            il.Emit(OpCodes.Ldelema, elemType);

            GenDeserializerCall(ctx, il, elemType);

            // i = i + 1
            il.Emit(OpCodes.Ldloc_S, idxLocal);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Add);
            il.Emit(OpCodes.Stloc_S, idxLocal);

            il.MarkLabel(loopCheckLabel);

            // loop condition
            il.Emit(OpCodes.Ldloc_S, idxLocal);
            il.Emit(OpCodes.Ldloc_S, arrLocal);
            il.Emit(OpCodes.Ldlen);
            il.Emit(OpCodes.Conv_I4);
            il.Emit(OpCodes.Clt);
            il.Emit(OpCodes.Brtrue_S, loopBodyLabel);


            // store new array to the out value
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Ldloc_S, arrLocal);
            il.Emit(OpCodes.Stind_Ref);

            il.Emit(OpCodes.Ret);
        }
コード例 #36
0
 /// <summary>
 /// Emits the necessary instructions to execute a <see langword="callvirt"/> operation onto the stream of instructions
 /// </summary>
 /// <param name="il">The input <see cref="ILGenerator"/> instance to use to emit instructions</param>
 /// <param name="methodInfo">The <see cref="MethodInfo"/> instance representing the method to invoke</param>
 public static void EmitCallvirt(this ILGenerator il, MethodInfo methodInfo) => il.EmitCall(OpCodes.Callvirt, methodInfo, null);
コード例 #37
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, System.Reflection.Emit.ILGenerator il)
        {
            var convention = config.Conventions.OfType <TransactionProxyConvention>().First();

            var listType           = typeof(IEnumerable <>).MakeGenericType(config.BaseType);
            var enumeratorType     = typeof(IEnumerator <>).MakeGenericType(config.BaseType);
            var enumeratorVariable = il.DeclareLocal(enumeratorType);
            var currentVariable    = il.DeclareLocal(config.BaseType);

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, convention.TargetsField.Field);
            il.EmitCall(OpCodes.Callvirt, listType.GetMethod("GetEnumerator"), null);
            il.Emit(OpCodes.Stloc, enumeratorVariable);

            var loopLabel = il.DefineLabel();
            var nextLabel = il.DefineLabel();

            var tryBlock = il.BeginExceptionBlock();

            il.MarkLabel(loopLabel);
            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.EmitCall(OpCodes.Callvirt, typeof(IEnumerator).GetMethod("MoveNext"), null);
            il.Emit(OpCodes.Brfalse, nextLabel);

            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.EmitCall(OpCodes.Callvirt, enumeratorType.GetProperty("Current").GetGetMethod(), null);
            il.Emit(OpCodes.Stloc, currentVariable);

            foreach (var item in convention.TransactionProxyProperties)
            {
                var done = il.DefineLabel();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, item.HasMultipleValuesProperty.BackingField);
                il.Emit(OpCodes.Ldc_I4_1);
                il.Emit(OpCodes.Ceq);
                il.Emit(OpCodes.Brtrue_S, done);

                il.Emit(OpCodes.Ldloc, currentVariable);
                il.Emit(OpCodes.Ldarg_0);
                il.EmitCall(OpCodes.Callvirt, item.Property.GetGetMethod(true), null);
                il.EmitCall(OpCodes.Callvirt, item.Property.GetSetMethod(true), null);

                il.MarkLabel(done);
            }

            il.Emit(OpCodes.Br, loopLabel);

            var endFinally = il.DefineLabel();

            il.BeginFinallyBlock();
            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Ceq);
            il.Emit(OpCodes.Brtrue_S, endFinally);
            il.Emit(OpCodes.Ldloc, enumeratorVariable);
            il.EmitCall(OpCodes.Callvirt, typeof(IDisposable).GetMethod("Dispose"), null);
            il.MarkLabel(endFinally);
            il.EndExceptionBlock();

            il.MarkLabel(nextLabel);

            il.Emit(OpCodes.Ret);
        }
コード例 #38
0
ファイル: MethodBuilder.cs プロジェクト: m9ra/MEFEditor_v2.0
 /// <summary>
 /// Emit given method call in common CLR. (Expects arguments and this object on stack)
 /// </summary>
 /// <param name="methodInfo"></param>
 private void EmitCLRCall(MethodInfo methodInfo)
 {
     _g.EmitCall(OpCodes.Call, methodInfo, null);
 }