本文整理汇总了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;
}
}
示例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
//.........这里部分代码省略.........