我有一些代码来加载一个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下使用单目文件)?
注意事项:我在Linux下使用MonoDevelop 2.6,或者在windows 7下使用MonoDevelop 2.8。
发布于 2012-03-14 06:51:31
您似乎误解了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()存在的原因,但它已经被废弃了。
https://stackoverflow.com/questions/9687706
复制相似问题