1

我有一些代码可以加载 exe 文件并向用户显示其 CIL 代码。为此,我使用 Mono.Cecil 和 Mono.Cecil.Cil。

现在我想做一些不同的事情:我想知道用户的系统中是否有 Mono.Cecil 和 Mono.Cecil.Cil。为此,我想将 Reflection.Assembly.Load 与 Mono.Cecil 和 Mono.Cecil.Cil 一起使用。就像是:

public void PrintInstr( ) {
    try
    {
        Reflect.Assembly mc = Reflect.Assembly.Load( "Mono.Cecil" );
        Reflect.Assembly mcc = Reflect.Assembly.Load( "Mono.Cecil.Cil" );
    }
    catch( Exception )
    {
        System.Console.WriteLine( "\"Mono.Cecil\" or \"Mono.Cecil.Cil\" not found " );
        return;
    }
    //[...]
}

但我只收到以下错误:

Could not load file or assembly 'Mono.Cecil' or one of its dependencies.
The system cannot find the file specified.

当然,我有 Mono.Cecil 和 Mono.Cecil.Cil。我没有正确使用 Assembly.Load 吗?如果是这种情况,有人可以告诉我如何使用 Assembly.Load 来加载 Mono.Cecil 和 Mono.Cecil.Cil 而无需寻找路径(制作仅在 Windows 或 GNU/Linux 下使用的 exe 文件单声道)?

注意:我在 Linux Mint 下使用 MonoDevelop 2.6 或在 windows 7 下使用 MonoDevelop 2.8。

4

1 回答 1

3

您似乎误解了Assembly.Load加载程序集的作用。我想您要查找的是用户是否在 GAC 中有 Mono.Cecil。问题是当您提供部分名称时,仅搜索当前 AppDomain 的搜索路径,仅当您提供全名时才使用 GAC。这记录在这里

不建议为 assemblyRef 提供部分程序集名称。(部分名称省略了文化、版本或公钥标记中的一个或多个。对于采用字符串而不是 AssemblyName 对象的重载,“MyAssembly, Version=1.0.0.0”是部分名称的示例,“MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18ab3442da84b47" 是全名的示例。)使用部分名称会对性能产生负面影响。此外,仅当应用程序基目录(BaseDirectory 或 AppDomainSetup.ApplicationBase)中存在程序集的精确副本时,部分程序集名称才能从全局程序集缓存加载程序集。

有关如何为程序集使用 CLR 探针的更多信息,请访问:http: //msdn.microsoft.com/en-us/library/aa720133.aspx

这正是Assembly.LoadWithPartialName()存在的原因,但它已被弃用。

于 2012-03-14T06:51:31.190 回答