2014-02-14 28 views
1

我使用Mono.Cecil來加密我的程序集中的字符串。C#成員'System.Char [] System.String :: ToCharArray()'在另一個模塊中聲明,需要導入Mono.Cecil

myAssemblyDefinition.Write(myAssemblyPath); 

我得到一個錯誤:

Member 'System.Char[] System.String::ToCharArray()' is declared in another module and needs to be imported

我試圖導入String.ToCharArray方法與所有這些行:

myAssemblyDefinition.MainModule.Import(stringTypeReference.Resolve()); 
myAssemblyDefinition.MainModule.Import(stringTypeReference.Resolve().Module.Types.Where(x => x.Name == "String").First()); 
MethodDefinition toCharArrayMethod = stringTypeReference.Resolve().Module.Types.Where(x => x.Name == "String").First().Methods.Where(x => x.Name == "ToCharArray").First(); 
myAssemblyDefinition.MainModule.Import(toCharArrayMethod); 
myAssemblyDefinition.MainModule.Import(typeof(System.String)); 

但我的問題仍然存在。 我用ToCharArray方法將decryptMethod注入我的程序集。 有人可以幫我解決這個問題或 是否有一個示例代碼來加密字符串與Mono.Cecil 0.9.5版本混淆?

回答

2

如果您的目標程序集的運行時版本與您的注入代碼的運行時版本相同。 請嘗試以下幾行。

var toCharArray = 
asm.MainModule.Import(typeof(string).GetMethod("ToCharArray", new Type[] { })); 
proccer.Emit(OpCodes.Callvirt, toCharArray); 
+0

謝謝。 也是我的問題是我使用代碼從Mono.Cecil 0.6 insted Mono.Cecil 0.9 我解決了這個通過閱讀這個鏈接[https://github.com/jbevain/cecil/wiki/Importing](https:// github.com/jbevain/cecil/wiki/Importing) –

相關問題