本文整理汇总了C#中ModuleDefinition.Import方法的典型用法代码示例。如果您正苦于以下问题:C# ModuleDefinition.Import方法的具体用法?C# ModuleDefinition.Import怎么用?C# ModuleDefinition.Import使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModuleDefinition
的用法示例。
在下文中一共展示了ModuleDefinition.Import方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessMethod
private static void ProcessMethod(ModuleDefinition module, MethodDefinition method, CustomAttribute aspect)
{
method.Body.SimplifyMacros();
MethodReference aspectConstructorRef = aspect.Constructor;
MethodInfo getCurrentMethod = typeof(MethodBase).GetMethod("GetCurrentMethod", BindingFlags.Public | BindingFlags.Static);
MethodReference getCurrentMethodRef = module.Import(getCurrentMethod);
TypeReference methodInfoRef = module.Import(typeof(MethodInfo));
TypeReference objectRef = module.Import(typeof(Object));
MethodReference methodExecutionArgsCtorRef =
module.Import(typeof(MethodExecutionArgs).GetConstructor(new Type[]
{
typeof(MethodInfo), typeof(object[])
}));
MethodReference onEntryRef =
module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnEntry"));
MethodReference onSuccessRef =
module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnSuccess"));
MethodReference onExceptionRef =
module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnException"));
MethodReference onExitRef =
module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnExit"));
MethodInfo getFlowBehavior =
typeof(MethodExecutionArgs).GetProperty("FlowBehavior").GetGetMethod();
MethodReference getFlowBehaviorRef = module.Import(getFlowBehavior);
MethodInfo setException =
typeof(MethodExecutionArgs).GetProperty("Exception").GetSetMethod();
MethodReference setExceptionRef = module.Import(setException);
MethodInfo getException =
typeof(MethodExecutionArgs).GetProperty("Exception").GetGetMethod();
MethodReference getExceptionRef = module.Import(getException);
MethodInfo getParameters = typeof(MethodBase).GetMethod("GetParameters");
MethodReference getParametersRef = module.Import(getParameters);
var processor = method.Body.GetILProcessor();
var firstInstruction = method.Body.Instructions[0];
// Original last instruction, change to a Nop.
method.Body.Instructions.Last().OpCode = OpCodes.Nop;
var finalInstruction = processor.Create(OpCodes.Nop);
method.Body.Instructions.Add(finalInstruction);
method.Body.Instructions.Add(processor.Create(OpCodes.Ret));
// Add necessary variables
int existingVariableCount = method.Body.Variables.Count;
method.Body.Variables.Add(new VariableDefinition("__aspect", aspect.AttributeType)); // module.Import(aspect.GetType())));
method.Body.Variables.Add(new VariableDefinition("__methodInfo", module.Import(typeof(MethodInfo))));
method.Body.Variables.Add(new VariableDefinition("__methodExecutionArgs",
module.Import(typeof(MethodExecutionArgs))));
method.Body.Variables.Add(new VariableDefinition("__ex", module.Import(typeof(Exception))));
var argsVariableRef = new VariableDefinition("__args", module.Import(typeof(object[])));
method.Body.Variables.Add(argsVariableRef);
var boolVariableRef = new VariableDefinition("__bool", module.Import(typeof(bool)));
method.Body.Variables.Add(boolVariableRef);
var flowBehaviorVariableRef = new VariableDefinition("__flow", module.Import(typeof(FlowBehavior)));
method.Body.Variables.Add(flowBehaviorVariableRef);
Instruction L_0032_Instruction;
Instruction firstAfterInstruction = Instruction.Create(OpCodes.Nop);
const int Variable_aspect = 0;
const int Variable_methodInfo = 1;
const int Variable_methodExecutionArgs = 2;
const int Variable_ex = 3;
var beforeInstructions = new List<Instruction>
{
processor.Create(OpCodes.Nop),
processor.Create(OpCodes.Newobj, aspectConstructorRef),
processor.Create(OpCodes.Stloc, Variable_aspect + existingVariableCount),
processor.Create(OpCodes.Call, getCurrentMethodRef),
processor.Create(OpCodes.Isinst, methodInfoRef),
processor.Create(OpCodes.Stloc, Variable_methodInfo + existingVariableCount),
processor.Create(OpCodes.Ldloc, Variable_methodInfo + existingVariableCount),
};
beforeInstructions.AddRange(new List<Instruction> {
processor.Create(OpCodes.Ldc_I4, method.Parameters.Count), // Number of arguments in method
processor.Create(OpCodes.Newarr, objectRef), // Create the object array (sized to hold args)
processor.Create(OpCodes.Stloc_S, argsVariableRef), // Save array to variable
processor.Create(OpCodes.Ldloc_S, argsVariableRef), // Load variable for evaluation
});
for (int i = 0; i < method.Parameters.Count; i++)
//.........这里部分代码省略.........