本文整理汇总了C#中Mono.Cecil.AssemblyDefinition.ReadAssembly方法的典型用法代码示例。如果您正苦于以下问题:C# AssemblyDefinition.ReadAssembly方法的具体用法?C# AssemblyDefinition.ReadAssembly怎么用?C# AssemblyDefinition.ReadAssembly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.AssemblyDefinition
的用法示例。
在下文中一共展示了AssemblyDefinition.ReadAssembly方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Inject
public void Inject(Mono.Cecil.AssemblyDefinition assemblyDefinition,
Dictionary<Mono.Cecil.MethodDefinition, List<Aspect>> eligibleMethods)
{
/* The around aspect is to intercept all calls to the target method, and
* replace those calls with a completely different method. While preserving
* the option to call back to the target method when necessary.
*/
this.AssemblyDefinition = assemblyDefinition;
this.EligibleMethods = eligibleMethods;
var NewMethodNames = new StringCollection();
var eligibleAroundMethods = this.EligibleMethods.Where(x => x.Value.Any(y =>
y.BuffaloAspect == Common.Enums.BuffaloAspect.MethodAroundAspect));
foreach (var d in eligibleAroundMethods)
{
var method = d.Key;
var aspects = d.Value;
var il = method.Body.GetILProcessor();
var methodType = method.DeclaringType;
var maInstructions = new List<Instruction>();
var aspectVarInstructions = new List<Instruction>();
var aroundInstructions = new List<Instruction>();
foreach (var aspec in aspects.Where(x =>
x.BuffaloAspect == Common.Enums.BuffaloAspect.MethodAroundAspect))
{
//if aspect is from a different assembly, need to work from that context
var aspect = aspec;
var writedll = false;
AssemblyDefinition ass = null;
if (!aspect.TypeDefinition.Module.FullyQualifiedName.Equals(
this.AssemblyDefinition.MainModule.FullyQualifiedName))
{
ass = AssemblyDefinition.ReadAssembly(aspect.TypeDefinition.Module.FullyQualifiedName);
var asp = ass.MainModule.Types.FirstOrDefault(x => x.FullName == aspect.Name);
if (asp != null)
{
var newaspect = new Aspect { Name = asp.FullName, TypeDefinition = asp, BuffaloAspect = Common.Enums.BuffaloAspect.MethodAroundAspect };
aspect = newaspect;
writedll = true;
}
}
var varTicks = System.DateTime.Now.Ticks;
//create a replacement for the annotated function
var methodName = string.Format("{0}{1}", method.Name, varTicks);
MethodDefinition newmethod =
new MethodDefinition(methodName, method.Attributes, method.ReturnType);
methodType.Methods.Add(newmethod);
NewMethodNames.Add(methodName);
//newmethod.Body.SimplifyMacros();
newmethod.Body.InitLocals = true;
//create aspect variable
var varAspectName = "asp" + varTicks;
var varAspectRef = this.AssemblyDefinition.MainModule.Import(aspect.TypeDefinition);
var varAspect = new VariableDefinition(varAspectName, varAspectRef);
newmethod.Body.Variables.Add(varAspect);
var varAspectIdx = newmethod.Body.Variables.Count - 1;
var ctor = aspect.TypeDefinition.Methods.First(x => x.IsConstructor);
var ctoref = this.AssemblyDefinition.MainModule.Import(ctor);
//store the newly created aspect variable
newmethod.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, ctoref));
newmethod.Body.Instructions.Add(Instruction.Create(OpCodes.Stloc, varAspect));
//copy all the paramters
method.Parameters.ToList().ForEach(x =>
newmethod.Parameters.Add(new ParameterDefinition(x.Name, x.Attributes, x.ParameterType)));
//create a MethodArgs
var var = newmethod.AddMethodArgsVariable(this.AssemblyDefinition);
#region Calling MethodArgs.Invoke
newmethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, varAspect));
newmethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, var.Var));
var aspectInvoke = aspect.TypeDefinition.Methods.First(x => x.Name.Equals("Invoke"));
var aspectInvokeRef =
this.AssemblyDefinition.MainModule.Import(aspectInvoke, newmethod);
newmethod.Body.Instructions.Add(Instruction.Create(OpCodes.Callvirt, aspectInvokeRef));
#endregion
#region Handling return value
if (!newmethod.ReturnType.FullName.Equals("System.Void"))
{
//create an object variable to hold the return value
var varObj = new VariableDefinition("obj" + varTicks,
this.AssemblyDefinition.MainModule.Import(typeof(object)));
newmethod.Body.Variables.Add(varObj);
newmethod.Body.Instructions.Add(
Instruction.Create(OpCodes.Stloc, varObj));
newmethod.Body.Instructions.Add(
Instruction.Create(OpCodes.Ldloc, varObj));
newmethod.Body.Instructions.Add(
Instruction.Create(OpCodes.Unbox_Any, newmethod.ReturnType));
}
else
{
//pop the return value since it's not used?
newmethod.Body.Instructions.Add(
Instruction.Create(OpCodes.Pop));
}
#endregion
//.........这里部分代码省略.........