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


C# TypeDefinition.MakeGeneric方法代码示例

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


在下文中一共展示了TypeDefinition.MakeGeneric方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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.TypeDefinition.MakeGeneric方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。