锁定的目标组件,带Mono.Cecil能和PCL代码注入

问题描述:

我有一个解决方案结构如下:锁定的目标组件,带Mono.Cecil能和PCL代码注入

**CoreSolution** 
| 
+---- Core.dll (PCL) 
     | 
     +---- CodeInjectionLogic (Inserts IL instruction on each type using Mono.Cecil for PCL) 

**BuildSolution** 
| 
+---- Core.dll (For Project Reference) 
|  
+---- CustomMSBuildTask.dll (Injects the code into the target.dll) 
     | 
     + ---CodeInjectionTask 
      Applies CodeInjectionLogic on each Type to weave IL instruction 

**TargetSolution** 
| 
+---- Core.dll (For Project Reference) 
| 
+---- Target.dll (PCL) 
     | 
     + <using Task CodeInjectionTask....> 

为CustomMSBuildTask.dll锁定问题是通过复制在作为BeforeBuild事件的临时董事的所有DLL解决。

构建包含目标程序集并使用Mono.Cecil的TargetSolution我能够读取Target.dll修改类型并插入IL指令,但是当我尝试使用Mono.Cecil.AssemblyDefiniyion写回修改的流时。写()我总是从MSBuild得到一个错误

该进程无法访问Target.dll,因为它正在被另一个进程使用。我认为这是MSBuild本身。

关于如何使用Mono.Ceeil和PCL编译使用自定义MSBuild AfterBuild目标构建的目标程序集的任何指针。

好的,我问了这个问题Jb Evain,并根据他的评论我要回答我自己的问题。

这是由于在塞西尔的最新版本(0.10测试版)http://cecil.pe/post/149243207656/mono-cecil-010-beta-1

的重大更改。如果我们读取和写入同一个文件,然后我们必须如下更新代码。

// ReaderParameters { ReadWrite = true } is necessary to later write the file 
using (var module = ModuleDefinition.ReadModule(file, new ReaderParameters { ReadWrite = true })) 
{ 
    // Modify the assembly 
    module.Write(); // Write to the same file that was used to open the file 
}