当前位置: 首页>>代码示例>>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;未经允许,请勿转载。