當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。