【问题标题】:Replace method's Body with Body of another method using Mono.Cecil?使用 Mono.Cecil 将方法的主体替换为另一种方法的主体?
【发布时间】:2017-09-05 10:13:35
【问题描述】:

使用Mono.Cecil,当我们可以将目标MethodDefinitionBody 设置为源MethodDefinitionBody 时,看起来非常简单。对于简单的方法,这工作正常。但是对于某些使用自定义类型的方法(例如初始化一个新对象),它将不起作用(在写回程序集时抛出异常)。

这是我的代码:

//in current app
public class Form1 {
  public string Test(){
   return "Modified Test";
  }
}
//in another assembly
public class Target {
  public string Test(){
    return "Test";
  }
}

//the copying code, this works for the above pair of methods
//the context here is of course in the current app
var targetAsm = AssemblyDefinition.ReadAssembly("target_path");
var mr1 = targetAsm.MainModule.Import(typeof(Form1).GetMethod("Test"));
var targetType = targetAsm.MainModule.Types.FirstOrDefault(e => e.Name == "Target");
var m2 = targetType.Methods.FirstOrDefault(e => e.Name == "Test");
var m1 = mr1.Resolve();
var m1IL = m1.Body.GetILProcessor();

foreach(var i in m1.Body.Instructions.ToList()){
   var ci = i;
   if(i.Operand is MethodReference){
      var mref = i.Operand as MethodReference;
      ci = m1IL.Create(i.OpCode, targetType.Module.Import(mref));
   }
   else if(i.Operand is TypeReference){
      var tref = i.Operand as TypeReference;
      ci = m1IL.Create(i.OpCode, targetType.Module.Import(tref));
   }
   if(ci != i){
       m1IL.Replace(i, ci);
   }
}
//here the source Body should have its Instructions set imported fine
//so we just need to set its Body to the target's Body
m2.Body = m1.Body;
//finally write to another output assembly
targetAsm.Write("modified_target_path");

上面的代码没有从任何地方引用,我只是自己尝试了一下,发现它适用于简单的情况(例如我上面发布的两种方法Test)。但是如果源方法(在当前应用中定义)包含一些类型引用(比如一些构造函数 init ...),像这样:

public class Form1 {
  public string Test(){
   var u = new Uri("SomeUri");
   return u.AbsolutePath;
  }
}

然后它将在写回程序集时失败。抛出的异常是ArgumentException,并带有以下消息:

“成员'System.Uri'在另一个模块中声明,需要导入”

事实上,我之前也遇到过类似的消息,但它是针对方法调用的,例如 (string.Concat)。这就是我尝试导入MethodReference 的原因(您可以在我发布的代码中的foreach 循环中看到if)。这确实适用于这种情况。

但是这种情况不同,我不知道如何正确导入使用/引用的类型(在这种情况下是System.Uri)。我知道应该使用Import 的结果,对于MethodReference,您可以看到结果用于替换每个InstructionOperand。但是对于这种情况下的类型参考,我完全不知道如何。

【问题讨论】:

  • 用调用新方法来替换body不是更简单吗?
  • @JeroenMostert 这里的源Test 方法只是一个简单的方法,实际上它可以是任何复杂的代码(包含几十行......)。因此,如果我们每次都手动将这些代码转换为Instructions,这将是困难且毫无意义的。我想使用现有方法替换另一个程序集中定义的另一个代码。我真的认为 Mono.Cecil 是可行的。
  • 不,我的意思是——您想要的方法体已经正确编译(包括类型和程序集引用以及整个 hoopla)。与其尝试将其移植到新的主体中,不如将Source.Test 方法主体替换为对Target.Test 的调用? (如果存在单独的程序集是一个问题,请先进行 ILMerge 它们。)无论源或目标有多复杂,这都会起作用。
  • @JeroenMostert 我想我不明白你的意思,我想替换的是Target.Test,但只是它的Body。因此,当Target 类(通过保存为新程序集进行修改后)在其他地方(不在我当前的应用程序中)使用时,Test 方法将做我想要的(通过定义一个假方法Test - 以及作为源方法 - 在当前应用中)。
  • 我认为你必须使用 reflexil.net 和 .Net Reflector 来完成它。

标签: c# reflection cil reflection.emit mono.cecil


【解决方案1】:

我在问题中发布的所有代码都很好,但还不够。实际上是异常消息:

“成员'System.Uri'在另一个模块中声明,需要导入”

抱怨VariableDefinitionVariableType。我只是导入指令而不是变量(只是从源代码MethodBody 完全引用)。所以解决方案是我们也需要以相同的方式导入变量(并且可能也导入ExceptionHandlers,因为ExceptionHandler 具有应该导入的CatchType)。 这里只是导入VariableDefinition的类似代码:

var vars = m1.Body.Variables.ToList();
m1.Body.Variables.Clear();
foreach(var v in vars){
   var nv = new VariableDefinition(v.Name, targetType.Module.Import(v.VariableType));
   m1.Body.Variables.Add(nv);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2022-11-01
    • 2016-07-09
    相关资源
    最近更新 更多