当前位置: 首页>>代码示例>>C#>>正文


C# MethodDefinition.MakeGeneric方法代码示例

本文整理汇总了C#中Mono.Cecil.MethodDefinition.MakeGeneric方法的典型用法代码示例。如果您正苦于以下问题:C# MethodDefinition.MakeGeneric方法的具体用法?C# MethodDefinition.MakeGeneric怎么用?C# MethodDefinition.MakeGeneric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mono.Cecil.MethodDefinition的用法示例。


在下文中一共展示了MethodDefinition.MakeGeneric方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RewriteOriginalMethodBody

        protected virtual void RewriteOriginalMethodBody(TypeDefinition coroutineType, MethodDefinition coroutineCtor)
        {
            // If this is a ctor, we need to preserve the base ctor call
            // FIXME: Will this always come first? In C# yes, but other languages... maybe not.
            if (method.IsConstructor) {
                int i = -1;
                int removeIndex = -1;
                while (method.Body.Instructions.Count > removeIndex) {
                    i++;
                    if (removeIndex == -1 && method.Body.Instructions [i].OpCode.Code == Code.Call) {
                        removeIndex = i + 1;
                        continue;
                    }
                    if (removeIndex != -1)
                        method.Body.Instructions.RemoveAt (removeIndex);
                }
            } else {
                method.Body.Instructions.Clear ();
            }
            method.Body.Variables.Clear ();

            //if (debug)
            //	method.CustomAttributes.Add (new CustomAttribute (module.Import (debuggerHidden)));

            var il = method.Body.GetILProcessor ();

            var arg = 0;
            if (method.HasThis) {
                arg = 1;
                il.Emit (OpCodes.Ldarg_0);
            }

            for (var i = 0; i < method.Parameters.Count; i++)
                il.Emit (OpCodes.Ldarg, arg++);

            if (method.HasGenericParameters || method.DeclaringType.HasGenericParameters) {

                var genericCtor = coroutineCtor.MakeGeneric (module.Import (coroutineType.MakeGeneric (method.DeclaringType.GenericParameters.Concat (method.GenericParameters).ToArray ())));
                il.Emit (OpCodes.Newobj, module.Import (genericCtor));

            } else {
                il.Emit (OpCodes.Newobj, coroutineCtor);
            }

            if (method.ReturnType.IsVoid ())
                il.Emit (OpCodes.Pop);
            il.Emit (OpCodes.Ret);

            method.Body.ComputeOffsetsAndMaxStack ();
        }
开发者ID:chkn,项目名称:cirrus,代码行数:50,代码来源:AsyncMethodTransform.cs

示例2: InterceptMethod


//.........这里部分代码省略.........
            {
                il.Append(new[]
                              {
                                  il.Create(OpCodes.Ldloc_2),
                                  il.Create(OpCodes.Callvirt, Context.Importer.Import(typeof (Invocation), "get_Result"))
                                  ,
                                  il.Create(OpCodes.Stloc, 6)
                              });
            }

            // Interceptor: Check if invocation has been cancelled
            il.Append(new[]
                          {
                              il.Create(OpCodes.Ldloc_2),
                              il.Create(OpCodes.Callvirt, Context.Importer.Import(typeof (Invocation), "get_CanInvoke"))
                              ,
                              il.Create(OpCodes.Ldc_I4_0),
                              il.Create(OpCodes.Ceq),
                              il.Create(OpCodes.Stloc, 4),
                              il.Create(OpCodes.Ldloc, 4),
                              il.Create(OpCodes.Brtrue, endOfMethodInstruction)
                          });

            // Interceptor: Insert IL call from clone to renamed method
            if (!renamedMethod.IsStatic)
                il.Append(il.Create(OpCodes.Ldarg_0));

            //  Interceptor: Load args for method call
            foreach (var parameter in interceptorMethod.Parameters.ToList())
                il.Append(il.Create(OpCodes.Ldarg, parameter));

            if (renamedMethod.HasGenericParameters)
                il.Append(il.Create(OpCodes.Call,
                                    renamedMethod.MakeGeneric(interceptorMethod.GenericParameters.ToArray())));
            else
                il.Append(il.Create(OpCodes.Call, renamedMethod));

            // Interceptor: Store method return value
            if (interceptorMethod.ReturnType.Name != "Void")
                il.Append(il.Create(OpCodes.Stloc, 6));

            // Interceptor: Set return type on MethodInvocation
            if (interceptorMethod.ReturnType.Name != "Void")
            {
                if (renamedMethod.ReturnType.IsValueType)
                {
                    il.Append(new[]
                                  {
                                      il.Create(OpCodes.Ldloc_2),
                                      il.Create(OpCodes.Ldloc, 6),
                                      il.Create(OpCodes.Box, renamedMethod.ReturnType),
                                      il.Create(OpCodes.Callvirt,
                                                Context.Importer.Import(typeof (Invocation), "set_Result"))
                                  });
                }
                else
                {
                    il.Append(new[]
                                  {
                                      il.Create(OpCodes.Ldloc_2),
                                      il.Create(OpCodes.Ldloc, 6),
                                      il.Create(OpCodes.Callvirt,
                                                Context.Importer.Import(typeof (Invocation), "set_Result"))
                                  });
                }
            }
开发者ID:fir3pho3nixx,项目名称:cryo-aop,代码行数:67,代码来源:Property.cs

示例3: ProcessClosure

        private ClosureInfo ProcessClosure(AssemblyProcessorContext context, TypeDefinition closureType, TypeReference[] genericParameters)
        {
            ClosureInfo closure;
            if (closures.TryGetValue(closureType, out closure))
                return closure;

            var closureTypeConstructor = closureType.Methods.FirstOrDefault(x => x.Name == ".ctor");
            var closureGenericType = closureType.MakeGenericType(genericParameters);

            // Create factory method for pool items
            var factoryMethod = new MethodDefinition("<Factory>", MethodAttributes.HideBySig | MethodAttributes.Private | MethodAttributes.Static, closureGenericType);
            closureType.Methods.Add(factoryMethod);
            factoryMethod.Body.InitLocals = true;
            factoryMethod.Body.Variables.Add(new VariableDefinition(closureGenericType));
            var factoryMethodProcessor = factoryMethod.Body.GetILProcessor();
            // Create and store closure
            factoryMethodProcessor.Emit(OpCodes.Newobj, closureTypeConstructor.MakeGeneric(genericParameters));
            factoryMethodProcessor.Emit(OpCodes.Stloc_0);
            //// Return closure
            factoryMethodProcessor.Emit(OpCodes.Ldloc_0);
            factoryMethodProcessor.Emit(OpCodes.Ret);

            // Create pool field
            var poolField = new FieldDefinition("<pool>", FieldAttributes.Public | FieldAttributes.Static, poolType.MakeGenericType(closureGenericType));
            closureType.Fields.Add(poolField);
            var poolFieldReference = poolField.MakeGeneric(genericParameters);

            // Initialize pool
            var cctor = GetOrCreateClassConstructor(closureType);
            var ilProcessor2 = cctor.Body.GetILProcessor();
            var retInstruction = cctor.Body.Instructions.FirstOrDefault(x => x.OpCode == OpCodes.Ret);
            ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Ldnull));
            ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Ldftn, factoryMethod.MakeGeneric(genericParameters)));
            ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Newobj, funcConstructor.MakeGeneric(closureGenericType)));
            ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Newobj, poolConstructor.MakeGeneric(closureGenericType)));
            ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Stsfld, poolFieldReference));

            // Implement IPooledClosure
            closureType.Interfaces.Add(pooledClosureType);

            // Create reference count field
            var countField = new FieldDefinition("<referenceCount>", FieldAttributes.Public, context.Assembly.MainModule.TypeSystem.Int32);
            closureType.Fields.Add(countField);
            var oountFieldReference = countField.MakeGeneric(genericParameters);

            // Create AddReference method
            var addReferenceMethod = new MethodDefinition("AddReference", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot, context.Assembly.MainModule.TypeSystem.Void);
            var ilProcessor4 = addReferenceMethod.Body.GetILProcessor();
            ilProcessor4.Emit(OpCodes.Ldarg_0);
            ilProcessor4.Emit(OpCodes.Ldflda, oountFieldReference);
            ilProcessor4.Emit(OpCodes.Call, interlockedIncrementMethod);
            ilProcessor4.Emit(OpCodes.Pop);
            ilProcessor4.Emit(OpCodes.Ret);
            closureType.Methods.Add(addReferenceMethod);

            // Create Release method
            var releaseMethod = new MethodDefinition("Release", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot, context.Assembly.MainModule.TypeSystem.Void);
            ilProcessor4 = releaseMethod.Body.GetILProcessor();
            retInstruction = ilProcessor4.Create(OpCodes.Ret);
            // Check decremented reference count
            ilProcessor4.Emit(OpCodes.Ldarg_0);
            ilProcessor4.Emit(OpCodes.Ldflda, oountFieldReference);
            ilProcessor4.Emit(OpCodes.Call, interlockedDecrementMethod);
            ilProcessor4.Emit(OpCodes.Ldc_I4_0);
            ilProcessor4.Emit(OpCodes.Ceq);
            ilProcessor4.Emit(OpCodes.Brfalse_S, retInstruction);
            // Release this to pool
            ilProcessor4.Emit(OpCodes.Ldsfld, poolFieldReference);
            ilProcessor4.Emit(OpCodes.Ldarg_0);
            ilProcessor4.Emit(OpCodes.Callvirt, poolReleaseMethod.MakeGeneric(closureGenericType));
            ilProcessor4.Append(retInstruction);
            closureType.Methods.Add(releaseMethod);

            closures.Add(closureType, closure = new ClosureInfo
            {
                FactoryMethod = factoryMethod,
                AddReferenceMethod = addReferenceMethod,
                ReleaseMethod = releaseMethod,
                PoolField = poolField
            });

            return closure;
        }
开发者ID:Kryptos-FR,项目名称:xenko-reloaded,代码行数:83,代码来源:DispatcherProcessor.cs

示例4: TransformAsyncMethod

        // FIXME: This method should be refactored into its own class it's so hefty!
        protected virtual void TransformAsyncMethod(MethodDefinition method)
        {
            if (method.Parameters.Any (p => p.IsOut))
                throw new Error (method.Module.Name,
                                 string.Format ("method `{0}' in type `{1}' cannot be transformed into an asynchronous coroutine becuase it has a ref or out parameter",
                                                method.Name, method.DeclaringType.Name));

            // Create Future implementation...
            var module = method.Module;
            var containingType = method.DeclaringType;

            var voidType   = module.Import (typeof (void));
            var intType    = module.Import (typeof (int));

            var isScheduled = module.Import (typeof (Future).GetProperty ("IsScheduled", BindingFlags.Public | BindingFlags.Instance).GetGetMethod ());
            var schedule    = module.Import (typeof (Future).GetMethod ("Schedule", new Type [] { typeof (Thread) }));

            var getException = module.Import (typeof (Future).GetProperty ("Exception").GetGetMethod ());
            var setException = module.Import (typeof (Future).GetProperty ("Exception").GetSetMethod ());

            var getStatus = module.Import (typeof (Future).GetProperty ("Status").GetGetMethod ());
            var setStatus = module.Import (typeof (Future).GetProperty ("Status").GetSetMethod ());

            TypeReference futureValueType = null;
            TypeReference baseType = null;

            MethodReference baseCtor = null;
            MethodReference chain = null;

            FieldDefinition [] argsFields;
            FieldDefinition [] localsFields;

            FieldReference threadFld = null;
            FieldReference chainedFld = null;
            FieldReference pcFld = null;

            VariableDefinition dupLoc = null;

            // If the method or containing type is generic, we have to account for that...

            var typeGeneric = new GenericParameter [containingType.GenericParameters.Count];
            var methodGeneric = new GenericParameter [method.GenericParameters.Count];

            var skews = new Dictionary<IGenericParameterProvider,int> (2);
            skews.Add (containingType, 0);
            skews.Add (method, typeGeneric.Length);

            int i = typeGeneric.Length + methodGeneric.Length;

            var futureName = string.Format ("__cirrus{0}_{1}_{2}_impl", asyncMethodID++, method.Name.Replace ("`", "$"),
                                            string.Join ("_", method.Parameters.Select (p => p.ParameterType.Name.Replace ("`", "$")).ToArray ()));
            if (i > 0)
                futureName += string.Format ("`{0}", i);

            var future = new TypeDefinition (null, futureName, Mono.Cecil.TypeAttributes.NestedPrivate | Mono.Cecil.TypeAttributes.Sealed);

            for (i = 0; i < typeGeneric.Length; i++) {
                typeGeneric [i] = new GenericParameter (containingType.GenericParameters [i].Name, future);
                future.GenericParameters.Add (typeGeneric [i]);
            }
            for (i = 0; i < methodGeneric.Length; i++) {
                methodGeneric [i] = new GenericParameter (method.GenericParameters [i].Name, future);
                future.GenericParameters.Add (methodGeneric [i]);
            }

            var returnType = method.ReturnType.CopyGeneric (future, skews);
            if (returnType.IsGenericInstance) { // returns Future<T>
                futureValueType = ((GenericInstanceType)returnType).GenericArguments [0].CopyGeneric (future, skews);
                baseType = module.Import (typeof (Cirrus.CoroutineFuture<>)).MakeGeneric (futureValueType);
                future.BaseType = baseType;
                baseCtor = module.Import (typeof (Cirrus.CoroutineFuture<>).GetConstructor (new Type [] {}), baseType);
                baseCtor.DeclaringType = baseType;

                threadFld = module.Import (typeof (Cirrus.CoroutineFuture<>).GetField ("thread", BindingFlags.Instance | BindingFlags.NonPublic), baseType);
                threadFld.DeclaringType = baseType;
                chainedFld = module.Import (typeof (Cirrus.CoroutineFuture<>).GetField ("chained", BindingFlags.Instance | BindingFlags.NonPublic), baseType);
                chainedFld.DeclaringType = baseType;
                pcFld = module.Import (typeof (Cirrus.CoroutineFuture<>).GetField ("pc", BindingFlags.Instance | BindingFlags.NonPublic), baseType);
                pcFld.DeclaringType = baseType;

                chain = module.Import (typeof (Cirrus.CoroutineFuture<>).GetMethod ("Chain", BindingFlags.Instance | BindingFlags.NonPublic), baseType);
                chain.DeclaringType = baseType;

            } else { // returns Future or void...
                baseType = module.Import (typeof (Cirrus.CoroutineFuture));
                future.BaseType = baseType;
                baseCtor = module.Import (typeof (Cirrus.CoroutineFuture).GetConstructor (new Type [] {}));

                threadFld = module.Import (typeof (Cirrus.CoroutineFuture).GetField ("thread", BindingFlags.Instance | BindingFlags.NonPublic));
                chainedFld = module.Import (typeof (Cirrus.CoroutineFuture).GetField ("chained", BindingFlags.Instance | BindingFlags.NonPublic));
                pcFld = module.Import (typeof (Cirrus.CoroutineFuture).GetField ("pc", BindingFlags.Instance | BindingFlags.NonPublic));

                chain = module.Import (typeof (Cirrus.CoroutineFuture).GetMethod ("Chain", BindingFlags.Instance | BindingFlags.NonPublic));

            }
            containingType.NestedTypes.Add (future);

            // create ctor
            var ctor = new MethodDefinition (".ctor", Mono.Cecil.MethodAttributes.SpecialName | Mono.Cecil.MethodAttributes.RTSpecialName |
//.........这里部分代码省略.........
开发者ID:chkn,项目名称:cirrus,代码行数:101,代码来源:TargetIL_old.cs


注:本文中的Mono.Cecil.MethodDefinition.MakeGeneric方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。