本文整理汇总了C#中MethodBody.GetILProcessor方法的典型用法代码示例。如果您正苦于以下问题:C# MethodBody.GetILProcessor方法的具体用法?C# MethodBody.GetILProcessor怎么用?C# MethodBody.GetILProcessor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodBody
的用法示例。
在下文中一共展示了MethodBody.GetILProcessor方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContinueProcessing
void ContinueProcessing()
{
body = Method.Body;
body.SimplifyMacros();
var ilProcessor = body.GetILProcessor();
var returnFixer = new ReturnFixer
{
Method = Method
};
returnFixer.MakeLastStatementReturn();
exceptionVariable = new VariableDefinition(ModuleWeaver.ExceptionType);
body.Variables.Add(exceptionVariable);
messageVariable = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
body.Variables.Add(messageVariable);
paramsArrayVariable = new VariableDefinition(ModuleWeaver.ObjectArray);
body.Variables.Add(paramsArrayVariable);
var tryCatchLeaveInstructions = Instruction.Create(OpCodes.Leave, returnFixer.NopBeforeReturn);
var methodBodyFirstInstruction = GetMethodBodyFirstInstruction();
var catchInstructions = GetCatchInstructions().ToList();
ilProcessor.InsertBefore(returnFixer.NopBeforeReturn, tryCatchLeaveInstructions);
ilProcessor.InsertBefore(returnFixer.NopBeforeReturn, catchInstructions);
var handler = new ExceptionHandler(ExceptionHandlerType.Catch)
{
CatchType = ModuleWeaver.ExceptionType,
TryStart = methodBodyFirstInstruction,
TryEnd = tryCatchLeaveInstructions.Next,
HandlerStart = catchInstructions.First(),
HandlerEnd = catchInstructions.Last().Next
};
body.ExceptionHandlers.Add(handler);
body.InitLocals = true;
body.OptimizeMacros();
}
示例2: AddInstruction
public void AddInstruction()
{
var object_ref = new TypeReference ("System", "Object", null, null, false);
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
var body = new MethodBody (method);
var il = body.GetILProcessor ();
var first = il.Create (OpCodes.Nop);
var second = il.Create (OpCodes.Nop);
body.Instructions.Add (first);
body.Instructions.Add (second);
Assert.IsNull (first.Previous);
Assert.AreEqual (second, first.Next);
Assert.AreEqual (first, second.Previous);
Assert.IsNull (second.Next);
}
示例3: CloneMethodBody
private MethodBody CloneMethodBody(MethodBody body, MethodDefinition source, MethodDefinition target)
{
var context = target.DeclaringType.Module;
var nb = new MethodBody(target)
{
MaxStackSize = body.MaxStackSize,
InitLocals = body.InitLocals,
CodeSize = body.CodeSize
};
var worker = nb.GetILProcessor();
foreach (var var in body.Variables)
nb.Variables.Add(new VariableDefinition(
var.Name, FixTypeImport(context, var.VariableType)));
foreach (var instr in body.Instructions)
{
var ni = new Instruction(instr.OpCode, OpCodes.Nop);
switch (instr.OpCode.OperandType)
{
case OperandType.InlineArg:
case OperandType.ShortInlineArg:
if (instr.Operand == body.ThisParameter)
ni.Operand = nb.ThisParameter;
else
{
var param = body.Method.Parameters.IndexOf((ParameterDefinition)instr.Operand);
ni.Operand = target.Parameters[param];
}
break;
case OperandType.InlineVar:
case OperandType.ShortInlineVar:
var var = body.Variables.IndexOf((VariableDefinition)instr.Operand);
ni.Operand = nb.Variables[var];
break;
case OperandType.InlineField:
ni.Operand = FixFieldImport(context, (FieldReference)instr.Operand);
break;
case OperandType.InlineMethod:
ni.Operand = FixMethodImport(context, (MethodReference)instr.Operand);
break;
case OperandType.InlineType:
ni.Operand = FixTypeImport(context, (TypeReference)instr.Operand);
break;
case OperandType.InlineTok:
if ((instr.Operand) is TypeReference)
ni.Operand = FixTypeImport(context, (TypeReference)instr.Operand);
else if ((instr.Operand) is FieldReference)
ni.Operand = FixFieldImport(context, (FieldReference)instr.Operand);
else if ((instr.Operand) is MethodReference)
ni.Operand = FixMethodImport(context, (MethodReference)instr.Operand);
break;
case OperandType.ShortInlineBrTarget:
case OperandType.InlineBrTarget:
case OperandType.InlineSwitch:
break;
default:
ni.Operand = instr.Operand;
break;
}
worker.Append(ni);
}
for (var i = 0; i < body.Instructions.Count; i++)
{
var instr = nb.Instructions[i];
var oldi = body.Instructions[i];
switch (instr.OpCode.OperandType)
{
case OperandType.InlineSwitch:
{
var olds = (Instruction[])oldi.Operand;
var targets = new Instruction[olds.Length];
for (var j = 0; j < targets.Length; j++)
targets[j] = GetInstruction(body, nb, olds[j]);
instr.Operand = targets;
}
break;
case OperandType.InlineBrTarget:
case OperandType.ShortInlineBrTarget:
instr.Operand = GetInstruction(body, nb, (Instruction)oldi.Operand);
break;
}
}
foreach (var eh in body.ExceptionHandlers)
{
var neh = new ExceptionHandler(eh.HandlerType)
{
TryStart = GetInstruction(body, nb, eh.TryStart),
TryEnd = GetInstruction(body, nb, eh.TryEnd),
HandlerStart = GetInstruction(body, nb, eh.HandlerStart),
HandlerEnd = GetInstruction(body, nb, eh.HandlerEnd)
};
//.........这里部分代码省略.........
示例4: AddUsing
private void AddUsing(MethodBody methodBody, Tuple<Instruction, Instruction> usingBlock)
{
var stloc = usingBlock.Item1.Previous;
while (stloc.OpCode == OpCodes.Nop)
stloc = stloc.Previous;
var variable = (VariableDefinition)stloc.Operand;
if (!variable.VariableType.HasInterface("System.IDisposable") || variable.VariableType.IsArray)
return;
var il = methodBody.GetILProcessor();
var disposeCall = il.Create(OpCodes.Callvirt, ModuleDefinition.ImportReference(typeof(IDisposable).GetMethod("Dispose")));
methodBody.OptimizeMacros();
methodBody.SimplifyMacros();
var tryStart = usingBlock.Item1;
var handlerEnd = usingBlock.Item2.Next;
Instruction leave;
if (usingBlock.Item1 == usingBlock.Item2)
{
// Empty using
leave = il.Create(OpCodes.Leave, usingBlock.Item1);
il.InsertBefore(usingBlock.Item2, leave);
tryStart = leave;
handlerEnd = leave.Next;
}
else if (usingBlock.Item2.OpCode == OpCodes.Br)
{
leave = il.Create(OpCodes.Leave, (Instruction)usingBlock.Item2.Operand);
il.Replace(usingBlock.Item2, leave);
}
else
{
leave = il.Create(OpCodes.Leave, usingBlock.Item2.Next);
il.InsertAfter(usingBlock.Item2, leave);
}
var firstPartOfFinally = il.Create(OpCodes.Ldloc, variable);
var endFinally = il.Create(OpCodes.Endfinally);
if (variable.VariableType.IsGenericParameter)
{
il.InsertAfter(leave,
firstPartOfFinally,
il.Create(OpCodes.Box, variable.VariableType),
il.Create(OpCodes.Brfalse, endFinally),
il.Create(OpCodes.Ldloca, variable),
il.Create(OpCodes.Constrained, variable.VariableType),
disposeCall,
endFinally
);
}
else
{
il.InsertAfter(leave,
firstPartOfFinally,
il.Create(OpCodes.Brfalse, endFinally),
il.Create(OpCodes.Ldloc, variable),
disposeCall,
endFinally
);
}
var handler = new ExceptionHandler(ExceptionHandlerType.Finally)
{
TryStart = tryStart,
TryEnd = firstPartOfFinally,
HandlerStart = firstPartOfFinally,
HandlerEnd = handlerEnd,
};
methodBody.ExceptionHandlers.Add(handler);
}
示例5: FixEarlyReturns
private void FixEarlyReturns(MethodBody body, List<int> earlyReturns)
{
var il = body.GetILProcessor();
var lastReturn = body.Instructions.Last();
foreach (var instruction in earlyReturns.Select(offset => body.Instructions.AtOffset(offset)))
{
il.Replace(instruction, il.Create(OpCodes.Br, lastReturn));
}
}
示例6: UnattachedMethodBody
public void UnattachedMethodBody()
{
var system_void = typeof (void).ToDefinition ();
var method = new MethodDefinition ("NewMethod", MethodAttributes.Assembly | MethodAttributes.Static, system_void);
var body = new MethodBody (method);
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ret);
method.Body = body;
Assert.AreEqual (body, method.Body);
}