简体   繁体   English

检查Mono.Cecil和Mono.Cecil.Cil

[英]Checking Mono.Cecil and Mono.Cecil.Cil

I have some code to load a exe file and shows its CIL code to user. 我有一些代码来加载exe文件并向用户显示其CIL代码。 To do it I use Mono.Cecil and Mono.Cecil.Cil. 要做到这一点,我使用Mono.Cecil和Mono.Cecil.Cil。

Now I wanna do something different: I wanna know if user has Mono.Cecil and Mono.Cecil.Cil in his system. 现在我想做一些不同的事情:我想知道用户是否在他的系统中有Mono.Cecil和Mono.Cecil.Cil。 to do that I thought to use Reflection.Assembly.Load with Mono.Cecil and Mono.Cecil.Cil. 要做到这一点,我想使用Reflection.Assembly.Load与Mono.Cecil和Mono.Cecil.Cil。 Something like: 就像是:

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;
    }
    //[...]
}

But I only get the following error: 但我只得到以下错误:

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

And, of course, I have Mono.Cecil and Mono.Cecil.Cil. 当然,我有Mono.Cecil和Mono.Cecil.Cil。 Am I not using properly Assembly.Load? 我没有正确使用Assembly.Load? If it's the case, can someone tell me how to use Assembly.Load to be able to load Mono.Cecil and Mono.Cecil.Cil without looking for a path ( to make an only exe file to be used under Windows or GNU/Linux with mono )? 如果是这种情况,有人可以告诉我如何使用Assembly.Load能够加载Mono.Cecil和Mono.Cecil.Cil而无需查找路径(在Windows或GNU / Linux下使用一个唯一的exe文件)单声道)?

Note : I'm working under Linux Mint with MonoDevelop 2.6 or under windows 7 with MonoDevelop 2.8. 注意 :我在Linux Mint下使用MonoDevelop 2.6或在Windows 7下使用MonoDevelop 2.8。

You seem to be misunderstanding what Assembly.Load does to load the assembly. 您似乎误解了Assembly.Load加载程序集的功能。 I guess what you're trying to look for is whether the user has Mono.Cecil in the GAC. 我想你想要寻找的是用户是否在GAC中使用Mono.Cecil。 The problem is thta only the search paths of the current AppDomain are searched when you supply a partial name, the GAC is only used when you supply the full name. 问题是当您提供部分名称时,仅搜索当前AppDomain的搜索路径,仅在您提供全名时使用GAC。 This is documented here : 在此处记录

Supplying a partial assembly name for assemblyRef is not recommended. 不建议为assemblyRef提供部分程序集名称。 (A partial name omits one or more of culture, version, or public key token. For overloads that take a string instead of an AssemblyName object, "MyAssembly, Version=1.0.0.0" is an example of a partial name and "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18ab3442da84b47" is an example of a full name.) Using partial names has a negative effect on performance. (部分名称省略了一个或多个culture,version或public key token。对于采用字符串而不是AssemblyName对象的重载,“MyAssembly,Version = 1.0.0.0”是部分名称和“MyAssembly”的示例, Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 18ab3442da84b47“是全名的示例。”使用部分名称会对性能产生负面影响。 In addition, a partial assembly name can load an assembly from the global assembly cache only if there is an exact copy of the assembly in the application base directory (BaseDirectory or AppDomainSetup.ApplicationBase). 此外,只有在应用程序基目录(BaseDirectory或AppDomainSetup.ApplicationBase)中存在程序集的精确副本时,部分程序集名称才能从全局程序集缓存加载程序集。

More information on how the CLR Probes for Assemblies can be found here: http://msdn.microsoft.com/en-us/library/aa720133.aspx 有关CLR探针如何在大会上找到的更多信息,请访问: http//msdn.microsoft.com/en-us/library/aa720133.aspx

This is exactly why Assembly.LoadWithPartialName() exists, but it has been deprecated. 这正是Assembly.LoadWithPartialName()存在的原因,但它已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM