本文整理汇总了C#中Mono.Cecil.Cil.ILProcessor.Create方法的典型用法代码示例。如果您正苦于以下问题:C# ILProcessor.Create方法的具体用法?C# ILProcessor.Create怎么用?C# ILProcessor.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.Cil.ILProcessor
的用法示例。
在下文中一共展示了ILProcessor.Create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: 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));
}
示例3: EmitStateClassCreation
private Instruction EmitStateClassCreation(TypeReference role, Instruction instructionBeforeCreation, ILProcessor il)
{
var current = instructionBeforeCreation;
var stateClassFieldReference = ResolveStateClassField(role);
// don't emit this code if the state field and property was not implemented
// TODO: this needs to be checked before, possibly at the conflict resolution stage
if (stateClassFieldReference != null) {
current = InsertAfter(il, current, il.Create(OpCodes.Ldarg_0));
current = InsertAfter(il, current, il.Create(OpCodes.Newobj, role.ResolveStateClassCtor()));
current = InsertAfter(il, current, il.Create(OpCodes.Stfld, stateClassFieldReference));
}
return current;
}
示例4: ProcessCachedStaticFieldPattern
private void ProcessCachedStaticFieldPattern(ILProcessor il, Instruction queryInvocation)
{
MethodReference predicateMethod = GetMethodReferenceFromStaticFieldPattern(queryInvocation);
il.InsertBefore(queryInvocation, il.Create(OpCodes.Ldtoken, predicateMethod));
// At this point the stack is like this:
// runtime method handle, delegate reference, ObjectContainer
il.Replace(queryInvocation,
il.Create(OpCodes.Call,
InstantiateGenericMethod(
_NativeQueryHandler_ExecuteInstrumentedStaticDelegateQuery,
GetQueryCallExtent(queryInvocation))));
}
示例5: AddConditionGoto
private Instruction AddConditionGoto(ILProcessor ilp, Instruction last, Instruction stateMachineStarting)
{
var shouldRunSynchronouslyMethod = _engine.GetMethod<Func<ActorCore, bool>> (a => a.ShouldRunSynchronously ());
var loadThis = ilp.Create (OpCodes.Ldarg_0);
var loadField = ilp.Create (OpCodes.Ldfld, _actorMixin);
var callMethod = ilp.Create (OpCodes.Call, shouldRunSynchronouslyMethod);
var gotoNext = ilp.Create (OpCodes.Brtrue_S, stateMachineStarting);
ilp.InsertAfter (last, loadThis);
ilp.InsertAfter (loadThis, loadField);
ilp.InsertAfter (loadField, callMethod);
ilp.InsertAfter (callMethod, gotoNext);
return gotoNext;
}
示例6: 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);
}
}
}
}
}
}
}
示例7: ProcessPredicateCreationPattern
private void ProcessPredicateCreationPattern(ILProcessor il, Instruction queryInvocation)
{
MethodReference predicateMethod = GetMethodReferenceFromInlinePredicatePattern(queryInvocation);
Instruction ldftn = GetNthPrevious(queryInvocation, 2);
il.InsertBefore(ldftn, il.Create(OpCodes.Dup));
il.InsertBefore(queryInvocation, il.Create(OpCodes.Ldtoken, predicateMethod));
// At this point the stack is like this:
// runtime method handle, delegate reference, target object, ObjectContainer
il.Replace(queryInvocation,
il.Create(OpCodes.Call,
InstantiateGenericMethod(
_NativeQueryHandler_ExecuteInstrumentedDelegateQuery,
GetQueryCallExtent(queryInvocation))));
}
示例8: MethodBodyPatcher
public MethodBodyPatcher(string methodName, MethodDefinition method)
{
_methodName = methodName;
_methodBody = method.Body;
_processor = _methodBody.GetILProcessor();
_realBodyStart = _methodBody.Instructions.First();
_realBodyEnd = _methodBody.Instructions.Last();
_markStart1BeforeCreateArgumentsArray = _processor.Create(OpCodes.Nop);
_markStart2BeforeCreateMethodExecutionArgs = _processor.Create(OpCodes.Nop);
_markStart3BeforeOnEntryCall = _processor.Create(OpCodes.Nop);
_markStart4BeforeRealBodyStartExceptionHandler = _processor.Create(OpCodes.Nop);
_markEnd1NewRealBodyEnd = _processor.Create(OpCodes.Nop);
_markEnd2BeforeOnExitCall = _processor.Create(OpCodes.Nop);
_markRetNew = EndsWithThrow ? _processor.Create(OpCodes.Throw) : _processor.Create(OpCodes.Ret);
}
示例9: ImplementProceed
protected override void ImplementProceed(MethodDefinition methodInfo, MethodBody methodBody, ILProcessor il, FieldReference methodInfoField, MethodReference proceed, Action<ILProcessor> emitProceedTarget, MethodReference proceedTargetMethod, OpCode proceedOpCode)
{
// If T is an interface, then we want to check if target is null; if so, we want to just return the default value
var targetNotNull = il.Create(OpCodes.Nop);
EmitProxyFromProceed(il);
il.Emit(OpCodes.Ldfld, ClassWeaver.Target); // Load "target" from "this"
il.Emit(OpCodes.Brtrue, targetNotNull); // If target is not null, jump below
CecilExtensions.CreateDefaultMethodImplementation(methodBody.Method, il);
il.Append(targetNotNull); // Mark where the previous branch instruction should jump to
base.ImplementProceed(methodInfo, methodBody, il, methodInfoField, proceed, emitProceedTarget, proceedTargetMethod, proceedOpCode);
}
示例10: ParseInstruction
public static Instruction ParseInstruction(ILProcessor processor, TypeDefinition type, XElement instrXML)
{
string fieldName = instrXML.Attribute("Field").Value;
FieldDefinition field = type.Fields.FirstOrDefault(f => f.Name == fieldName);
if (field == null)
{
Console.WriteLine("Couldn't find field named " + fieldName);
return null;
}
return processor.Create(OpCodes.Ldfld, field);
}
示例11: Initialize
/// <summary>
/// Emits the instructions that call <see cref="IInitialize.Initialize"/> on a given service instance.
/// </summary>
/// <param name="il"></param>
/// <param name="module">The host module.</param>
/// <param name="serviceInstance">The local variable that points to the current service instance.</param>
public void Initialize(ILProcessor il, ModuleDefinition module, VariableDefinition serviceInstance)
{
var body = il.Body;
var method = body.Method;
var declaringType = method.DeclaringType;
var targetField = GetTargetField(declaringType);
if (targetField == null)
return;
var initializeType = module.ImportType<IInitialize>();
il.Emit(OpCodes.Ldloc, serviceInstance);
il.Emit(OpCodes.Isinst, initializeType);
var initializeMethod = module.ImportMethod<IInitialize>("Initialize");
var skipInitializationCall = il.Create(OpCodes.Nop);
il.Emit(OpCodes.Brfalse, skipInitializationCall);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, targetField);
GetServiceHash(il, module, serviceInstance);
var containsMethod = module.ImportMethod<Dictionary<int, int>>("ContainsKey");
il.Emit(OpCodes.Callvirt, containsMethod);
il.Emit(OpCodes.Brtrue, skipInitializationCall);
// if (!__initializedServices.ContainsKey(currentService.GetHashCode()) {
il.Emit(OpCodes.Ldloc, serviceInstance);
il.Emit(OpCodes.Isinst, initializeType);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, initializeMethod);
// __initializedServices.Add(hashCode, 0);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, targetField);
GetServiceHash(il, module, serviceInstance);
il.Emit(OpCodes.Ldc_I4_1);
var addMethod = module.ImportMethod<Dictionary<int, int>>("Add");
il.Emit(OpCodes.Callvirt, addMethod);
il.Append(skipInitializationCall);
}
示例12: ParseInstruction
public static Instruction ParseInstruction(ILProcessor processor, TypeDefinition type, XElement instrXML)
{
string assemblyName = instrXML.Attribute("Assembly").Value;
string typeName = instrXML.Attribute("Type").Value;
string methodName = instrXML.Attribute("Method").Value;
// Get Assembly
AssemblyDefinition assembly = Program.GetAssembly(assemblyName);
if (assembly == null)
return null;
// Get Type
TypeDefinition typeDefinition = assembly.MainModule.GetType(typeName);
// Get Method
MethodDefinition methodDefinition = typeDefinition.Methods.Single(m => m.Name == methodName);
MethodReference methodReference =
Program.GetAssembly("Assembly-CSharp").MainModule.Import(methodDefinition);
// Generic Parameter Check
if (instrXML.HasElements)
{
List<TypeReference> genericParameters = new List<TypeReference>();
foreach (XElement genericParameter in instrXML.Elements("GenericParameter"))
{
var gPAssemblyName = genericParameter.Attribute("Assembly").Value;
var gPType = genericParameter.Attribute("Type").Value;
AssemblyDefinition gPAssembly = Program.GetAssembly(gPAssemblyName);
TypeDefinition gPTypeDefinition = gPAssembly.MainModule.GetType(gPType);
TypeReference gPTypeReference =
Program.GetAssembly("Assembly-CSharp").MainModule.Import(gPTypeDefinition);
genericParameters.Add(gPTypeReference);
}
methodReference = methodReference.MakeGeneric(genericParameters.ToArray());
}
// Return Instruction
return processor.Create(OpCodes.Call, methodReference);
}
示例13: ImplementProceed
protected override void ImplementProceed(MethodDefinition methodInfo, MethodBody methodBody, ILProcessor il, FieldReference methodInfoField, MethodReference proceed, Action<ILProcessor> emitProceedTarget, MethodReference proceedTargetMethod, OpCode proceedOpCode)
{
if (methodInfo.IsAbstract)
{
CecilExtensions.CreateDefaultMethodImplementation(methodInfo, il);
}
else
{
var targetNotNull = il.Create(OpCodes.Nop);
EmitProxyFromProceed(il);
il.Emit(OpCodes.Ldfld, target); // Load "target" from "this"
il.Emit(OpCodes.Brtrue, targetNotNull); // If target is not null, jump below
base.ImplementProceed(methodInfo, methodBody, il, methodInfoField, proceed, _ => EmitProxyFromProceed(il), callBaseMethod, OpCodes.Call);
il.Append(targetNotNull); // Mark where the previous branch instruction should jump to
base.ImplementProceed(methodInfo, methodBody, il, methodInfoField, proceed, emitProceedTarget, proceedTargetMethod, proceedOpCode);
}
}
示例14: ToNop
public static int ToNop(ILProcessor ilp, Instruction ins, bool sameSize)
{
if (ins == null) return 0;
int size = ins.GetSize();
ins.OpCode = OpCodes.Nop;
ins.Operand = null;
if (sameSize)
{
for (int i = 1; i < size; i++)
{
Instruction newIns = ilp.Create(OpCodes.Nop);
ilp.InsertAfter(ins, newIns);
}
}
else
{
size = 1;
}
return size;
}
示例15: GetMethod
public static void GetMethod(Instruction i, ILProcessor proc, ModuleDefinition baseModule)
{
if (i.Operand is FieldReference
&& ((FieldReference)i.Operand).FullName.Contains("Monocle.Sprite`1<System.Int32> TowerFall.AwardInfo/<>c__DisplayClass")
&& ((FieldReference)i.Operand).FullName.Contains("::sprite"))
{
Stored = Instruction.Create(OpCodes.Ldfld, i.Operand as FieldReference);
}
if (i.OpCode == OpCodes.Callvirt && i.Next.OpCode != OpCodes.Br_S
&& ((MethodReference)i.Operand).FullName == "System.Void Monocle.Sprite`1<System.Int32>::Add(T,System.Int32)")
{
TypeDefinition graphicsComponent = baseModule.GetType("Monocle.GraphicsComponent");
var fieldReference = graphicsComponent.Fields.Single(f => f.Name == "Zoom");
var instr = Instruction.Create(OpCodes.Stfld, fieldReference);
var ldinstr = Instruction.Create(i.Next.OpCode);
proc.InsertAfter(i, instr);
proc.InsertAfter(i, proc.Create(OpCodes.Ldc_R4, (float)0.7));
proc.InsertAfter(i, Stored);
proc.InsertAfter(i, ldinstr);
}
}