當前位置: 首頁>>代碼示例>>C#>>正文


C# MethodDefinition.Clone方法代碼示例

本文整理匯總了C#中Mono.Cecil.MethodDefinition.Clone方法的典型用法代碼示例。如果您正苦於以下問題:C# MethodDefinition.Clone方法的具體用法?C# MethodDefinition.Clone怎麽用?C# MethodDefinition.Clone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Mono.Cecil.MethodDefinition的用法示例。


在下文中一共展示了MethodDefinition.Clone方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: InsertMethod

		public MethodDefinition InsertMethod (MethodDefinition method, bool clone) {
			if (methods.ContainsKey (method.Name)) {
				Console.Error.WriteLine ("Error: duplicated method {0}", method.Name);
				return null;
			} else {
				if (clone) {
					method = method.Clone ();
				}
				methods [method.Name] = method;

				if (method.Name == ".start") {
					//TODO More sanity checks here...
					Console.WriteLine ("*** Method '.start' found!");
					entryPoint = method;
				} else {
					Console.WriteLine ("*** Method {0} is not named '.start'", method.Name);
				}

				return method;
			}
		}
開發者ID:transformersprimeabcxyz,項目名稱:cecil-old,代碼行數:21,代碼來源:InternalSymbolTable.cs

示例2: Rewrite

        private void Rewrite(TypeDefinition typeDefinition, MethodDefinition methodDefinition, List<MethodDefinition> addedMethods)
        {
            if (!methodDefinition.HasBody)
                return;

            ImportReferences();

            // Create an interceptor stub for the method.
            string stubFieldName = StaticInterceptorStub.GetStubFieldName(methodDefinition.Name);
            FieldDefinition stubFieldDefinition = new FieldDefinition(stubFieldName, staticInterceptorStubReference,
                FieldAttributes.Private | FieldAttributes.Static | FieldAttributes.NotSerialized);
            typeDefinition.Fields.Add(stubFieldDefinition);

            // Clone the original method and give it a new name.
            MethodDefinition targetMethodDefinition = methodDefinition.Clone();
            targetMethodDefinition.Overrides.Clear();
            targetMethodDefinition.Attributes = (methodDefinition.Attributes & MethodAttributes.Static)
                | MethodAttributes.Private | MethodAttributes.HideBySig;
            targetMethodDefinition.CallingConvention = MethodCallingConvention.Default;
            targetMethodDefinition.Name = StaticInterceptorStub.GetTargetMethodName(methodDefinition.Name);

            addedMethods.Add(targetMethodDefinition);

            // Replace the original method with a stub that calls the callback.
            MethodBody body = new MethodBody(methodDefinition);
            body.InitLocals = true;
            methodDefinition.Body = body;
            CilWorker worker = body.CilWorker;

            /*** Obtain the invocation, if needed ***/
            // Load the stub if it has been initialized.
            VariableDefinition stubVariableDefinition = new VariableDefinition(staticInterceptorStubReference);
            body.Variables.Add(stubVariableDefinition);

            worker.Emit(OpCodes.Ldsfld, stubFieldDefinition);
            worker.Emit(OpCodes.Dup);
            worker.Emit(OpCodes.Stloc, stubVariableDefinition);
            Instruction fastPathEntryBranch = worker.Emit(OpCodes.Brfalse, body.Instructions.Outside);

            /*** Slow path ***/
            // Copy arguments to an array.
            VariableDefinition argumentsVariableDefinition = null;

            if (methodDefinition.Parameters.Count != 0)
            {
                argumentsVariableDefinition = new VariableDefinition(objectArrayReference);
                body.Variables.Add(argumentsVariableDefinition);

                worker.Emit(OpCodes.Ldc_I4, methodDefinition.Parameters.Count);
                worker.Emit(OpCodes.Newarr, objectReference);
                worker.Emit(OpCodes.Stloc, argumentsVariableDefinition);

                foreach (ParameterDefinition param in methodDefinition.Parameters)
                {
                    if ((param.Attributes & ParameterAttributes.In) != 0)
                    {
                        worker.Emit(OpCodes.Ldloc, argumentsVariableDefinition);
                        worker.Emit(OpCodes.Ldc_I4, param.Sequence);
                        worker.Emit(OpCodes.Ldarg, param);
                        worker.Emit(OpCodes.Box, objectReference);
                        worker.Emit(OpCodes.Stelem_Ref);
                    }
                }
            }

            // Create the invocation.
            worker.Emit(OpCodes.Ldloc, stubVariableDefinition);
            if (methodDefinition.HasThis)
                worker.Emit(OpCodes.Ldarg_0);
            else
                worker.Emit(OpCodes.Ldnull);
            if (argumentsVariableDefinition == null)
                worker.Emit(OpCodes.Ldsfld, noArgumentsReference);
            else
                worker.Emit(OpCodes.Ldloc, argumentsVariableDefinition);
            worker.Emit(OpCodes.Newobj, staticInvocationConstructorReference);

            // Execute it (leaves the result on the stack).
            worker.Emit(OpCodes.Call, executeReference);

            // Copy any ref and out arguments back out of the invocation's array.
            if (argumentsVariableDefinition != null)
            {
                foreach (ParameterDefinition param in methodDefinition.Parameters)
                {
                    if ((param.Attributes & ParameterAttributes.Out) != 0)
                    {
                        worker.Emit(OpCodes.Ldloc, argumentsVariableDefinition);
                        worker.Emit(OpCodes.Ldc_I4, param.Sequence);
                        worker.Emit(OpCodes.Ldelem_Ref);
                        worker.Emit(OpCodes.Unbox_Any, param.ParameterType);
                        worker.Emit(OpCodes.Starg, param);
                    }
                }
            }

            // Unbox the result if needed and return.
            if (CecilUtils.IsVoid(methodDefinition.ReturnType.ReturnType))
                worker.Emit(OpCodes.Pop);
            else
//.........這裏部分代碼省略.........
開發者ID:dougrathbone,項目名稱:mbunit-v3,代碼行數:101,代碼來源:StaticModuleRewriter.cs


注:本文中的Mono.Cecil.MethodDefinition.Clone方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。