本文整理汇总了C#中Mono.Cecil.Cil.ILProcessor类的典型用法代码示例。如果您正苦于以下问题:C# ILProcessor类的具体用法?C# ILProcessor怎么用?C# ILProcessor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ILProcessor类属于Mono.Cecil.Cil命名空间,在下文中一共展示了ILProcessor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleOfParameter
void HandleOfParameter(Instruction instruction, ILProcessor ilProcessor)
{
//Info.OfMethod("AssemblyToProcess","MethodClass","InstanceMethod");
var methodNameInstruction = instruction.Previous;
var methodName = GetLdString(methodNameInstruction);
var typeNameInstruction = methodNameInstruction.Previous;
var typeName = GetLdString(typeNameInstruction);
var assemblyNameInstruction = typeNameInstruction.Previous;
var assemblyName = GetLdString(assemblyNameInstruction);
var typeDefinition = GetTypeDefinition(assemblyName, typeName);
var methodDefinition = typeDefinition.Methods.FirstOrDefault(x => x.Name == methodName);
if (methodDefinition == null)
{
throw new WeavingException($"Could not find method named '{methodName}'.");
}
var methodReference = ModuleDefinition.ImportReference(methodDefinition);
ilProcessor.Remove(typeNameInstruction);
assemblyNameInstruction.OpCode = OpCodes.Ldtoken;
assemblyNameInstruction.Operand = methodReference;
instruction.Operand = getMethodFromHandle;
ilProcessor.InsertAfter(instruction,Instruction.Create(OpCodes.Castclass,methodInfoType));
}
示例2: StoreStart
public override void StoreStart(ILProcessor ilProcessor)
{
if (Previous.Type.Resolve().IsValueType)
Previous.EmitAddress(ilProcessor);
else
Previous.Emit(ilProcessor);
}
示例3: GetServiceHash
/// <summary>
/// Emits a call that obtains the hash code for the current service instance.
/// </summary>
/// <param name="il">The <see cref="ILProcessor"/> that points to the method body.</param>
/// <param name="module">The target module.</param>
/// <param name="serviceInstance">The local variable that contains the service instance.</param>
private void GetServiceHash(ILProcessor il, ModuleDefinition module, VariableDefinition serviceInstance)
{
il.Emit(OpCodes.Ldloc, serviceInstance);
var getHashCodeMethod = module.ImportMethod<object>("GetHashCode");
il.Emit(OpCodes.Callvirt, getHashCodeMethod);
}
示例4: RejigFirstInstruction
/// <summary>
/// Need to do this so we retain pointer from original first instruction to new first instruction (nop)
/// for dbg file to point to it so VS debugger will step into weaved methods
/// </summary>
/// <param name="ilProcessor"></param>
/// <param name="firstInstruction"></param>
/// <returns></returns>
private static Instruction RejigFirstInstruction(ILProcessor ilProcessor, Instruction firstInstruction)
{
/*
From:
opcode operand <-- pdb first line pointer
To:
nop <-- pdb first line pointer
opcode operand <-- cloned second acting as first
*/
// clone first instruction which will be used as actual
var clonedSecond = CloneInstruction(firstInstruction);
clonedSecond.Offset++;
var sampleNop = ilProcessor.Create(OpCodes.Nop);
// change actual first instruction to NOP
firstInstruction.OpCode = sampleNop.OpCode;
firstInstruction.Operand = sampleNop.Operand;
// append second instruction which now is same as first one used to be at the start of this method
// and actual first one is nop
firstInstruction.Append(clonedSecond, ilProcessor);
// return cloned second as new first instruction
return clonedSecond;
}
示例5: ModifyCallScope
public void ModifyCallScope(MethodDefinition renamedMethod, MethodDefinition interceptorMethod, ILProcessor il,
MethodInterceptionScopeType interceptionScope)
{
if (interceptionScope == MethodInterceptionScopeType.Deep)
{
foreach (var module in Type.Assembly.Definition.Modules)
{
foreach (var type in module.Types.ToList())
{
if (type.Methods == null || type.Methods.Count == 0) continue;
foreach (var method in type.Methods.ToList())
{
if (Context.Marker.HasMarker(method, Method.MethodMarker)) continue;
if (method == null
|| method.Body == null
|| method.Body.Instructions == null
|| method.Body.Instructions.Count() == 0)
continue;
foreach (var instruction in method.Body.Instructions.ToList())
{
if (instruction.OpCode == OpCodes.Call && instruction.Operand == renamedMethod)
{
var processor = method.Body.GetILProcessor();
processor.InsertAfter(instruction, il.Create(OpCodes.Call, interceptorMethod));
processor.Remove(instruction);
}
}
}
}
}
}
}
示例6: Generate
public override void Generate(ILProcessor processor)
{
// Load the arguments.
foreach (VariableDefinition v in m_Parameters)
{
processor.Append(Instruction.Create(OpCodes.Ldloc, v));
}
// Call the delegate.
processor.Append(Instruction.Create(OpCodes.Call, this.m_Target));
// Handle the return type.
if (this.m_ReturnType.FullName == this.m_Target.Module.Import(typeof(void)).FullName)
{
// Return value is void. Discard any result and return.
processor.Append(Instruction.Create(OpCodes.Pop));
}
else if (this.m_ReturnType.IsValueType || this.m_ReturnType.IsGenericParameter)
{
// Return value is value type (not reference). Unbox and return it.
processor.Append(Instruction.Create(OpCodes.Unbox_Any, this.m_ReturnType));
processor.Append(Instruction.Create(OpCodes.Stloc, this.Result));
}
else
{
// Return value is reference type. Cast it and return it.
processor.Append(Instruction.Create(OpCodes.Isinst, this.m_ReturnType));
processor.Append(Instruction.Create(OpCodes.Stloc, this.Result));
}
}
示例7: EmitCodeInit
private Instruction EmitCodeInit(TypeReference role, Instruction instructionBeforeInit, ILProcessor il)
{
var current = instructionBeforeInit;
current = InsertAfter(il, current, il.Create(OpCodes.Ldarg_0));
current = InsertAfter(il, current, il.Create(OpCodes.Call, ResolveInitReference(role)));
return current;
}
示例8: CompileCil
private void CompileCil(ILProcessor body, IAstElement element, CilCompilationContext context)
{
var compiler = this.cilCompilers.SingleOrDefault(c => c.CanCompile(body, element));
if (compiler == null)
throw new NotImplementedException("LightCompiler: No CilCompiler for " + element);
compiler.Compile(body, element, context);
}
示例9: EmitAddress
public override void EmitAddress(ILProcessor ilProcessor)
{
if (Previous.Type.Resolve().IsValueType)
Previous.EmitAddress(ilProcessor);
else
Previous.Emit(ilProcessor);
ilProcessor.Emit(OpCodes.Ldflda, Field);
}
示例10: AssemblyResolveUpdater
public AssemblyResolveUpdater(ModuleDefinition module)
{
_module = module;
var type = _module.GetType("TheIndex", "Resolver");
_initMethod = type.Methods.Single(m => m.Name == "DictionaryInitialization");
_addResolveMethod = type.Methods.Single(m => m.Name == "Add");
_proc = _initMethod.Body.GetILProcessor();
}
示例11: InjectWriteIl
static void InjectWriteIl(List<Instruction> writeTimeIl, ILProcessor ilProcessor, Instruction beforeThis)
{
foreach (var instruction in writeTimeIl)
{
ilProcessor.InsertBefore(beforeThis, instruction);
}
ilProcessor.InsertBefore(beforeThis, Instruction.Create(OpCodes.Endfinally));
}
示例12: IsPredicated
public Boolean IsPredicated(Instruction instruction, ILProcessor ilProcessor) {
try {
return predicate(instruction, ilProcessor);
}
catch (Exception) {
return false;
}
}
示例13: InsertAfter
public Instruction InsertAfter(Instruction instruction, ILProcessor processor)
{
var currentInstruction = instruction;
foreach (var newInstructionBlock in InstructionBlocks)
currentInstruction = newInstructionBlock.InsertAfter(currentInstruction, processor);
return currentInstruction;
}
示例14: MethodPatcher
public MethodPatcher(PatcherObject prnt, MethodDefinition metDef) : base(prnt)
{
methodDefinition = metDef;
IlProc = metDef.Body.GetILProcessor();
rootAssemblyPatcher = prnt.rootAssemblyPatcher;
if (MainClass.gendiffs && MainClass.newAssCS)
original = metDef.Print();
}
示例15: InterceptMethod
private void InterceptMethod(ILProcessor processor, TypeReference typeReference, Instruction instruction, string name)
{
var typeDefinition = typeReference.Resolve();
var attributeConstructor = typeDefinition.Methods.First(x => x.Name == ".ctor");
var attributeMethod = typeDefinition.Methods.First(x => x.Name == name);
processor.InsertBefore(instruction, processor.Create(OpCodes.Newobj, attributeConstructor));
processor.InsertBefore(instruction, processor.Create(OpCodes.Call, _getCurrentMethod));
processor.InsertBefore(instruction, processor.Create(OpCodes.Call, attributeMethod));
}