2017-01-24 23 views
0

我有一個解決方案結構如下:鎖定的目標組件,帶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目標構建的目標程序集的任何指針。

回答

1

好的,我問了這個問題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 
}