简体   繁体   English

从GAC动态加载最新的程序集版本

[英]Load latest assembly version dynamically from GAC

I'd like to dynamically load the latest installed version of an assembly in the GAC using reflections. 我想使用反射在GAC中动态加载程序集的最新安装版本。

So far I've found multiple working ways to accomplish this but they're all having their specific downsides. 到目前为止,我已经找到了实现这一目标的多种工作方法,但是它们都有其特定的缺点。

The easiest solution is using the Assembly.LoadWithPartialName() method. 最简单的解决方案是使用Assembly.LoadWithPartialName()方法。 But this method is obsolete since .NET Framework 2: 但是从.NET Framework 2开始,此方法已过时:

var assembly = Assembly.LoadWithPartialName("Microsoft.WindowsAzure.ServiceRuntime");

Another possibility is to use Assembly.Load() (as recommended by the obsolete warning) and call the different assembly versions with their fully qualified assembly name in a try/catch block to get the latest installed version. 另一种可能性是使用Assembly.Load() (如过时的警告所建议),并在try / catch块中调用具有完全限定的程序集名称的不同程序集版本,以获取最新的安装版本。 This screams for maintenance and just looks dirty: 这尖叫维护,只是看起来很脏:

Assembly assembly = null;

try
{
    assembly = Assembly.Load("Microsoft.WindowsAzure.ServiceRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
}
catch (FileNotFoundException) { }

try
{
    assembly = Assembly.Load("Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
}
catch (FileNotFoundException) { }

Last but not least there's another solution I found here on SO using the Assembly.LoadFrom() method and then basically importing the assembly manager module fusion.dll to figure the path of the latest version. 最后但并非最不重要的一点是,我在这里找到了另一个解决方案 ,即使用Assembly.LoadFrom()方法,然后基本上导入了程序集管理器模块fusion.dll来确定最新版本的路径。 This seems to be way too much for such a "simple" task. 对于这样的“简单”任务来说,这似乎太多了。

Isn't there any better solution to accomplish that without using an obsolete method, creating a maintenance hell with magic strings or by calling unmanaged code? 在没有使用过时的方法,使用魔术字符串创建维护地狱或通过调用非托管代码的情况下,还有没有更好的解决方案来实现这一目标?

Thanks in advance! 提前致谢!

Fusion.dll... ugh. Fusion.dll ...呃。 I would find your target framework version and enumerate all of the gas directories and simply query them based on the root assembly name so that your only magic string is the assembly name. 我会找到您的目标框架版本并枚举所有gas目录,并仅根据根程序集名称查询它们,以便您唯一的魔术字符串是程序集名称。 Then, use Assembly.ReflectionOnlyLoadFrom static method to compare the versions of until you find the latest one and then use the Assembly.Load method with the appropriate file path arguments. 然后,使用Assembly.ReflectionOnlyLoadFrom静态方法比较两个版本,直到找到最新版本为止,然后将Assembly.Load方法与相应的文件路径参数一起使用。 You may have trouble with this using click-once download unless you have given the app the appropriate trust. 除非您给予应用适当的信任,否则单击一次下载可能会给您带来麻烦。

// List of all the different types of GAC folders for both 32bit and 64bit
// environments.
List<string> gacFolders = new List<string>() { 
    "GAC", "GAC_32", "GAC_64", "GAC_MSIL", 
    "NativeImages_v2.0.50727_32", 
    "NativeImages_v2.0.50727_64" 
};

foreach (string folder in gacFolders)
{
    string path = Path.Combine(@"c:\windows\assembly", folder);
    if(Directory.Exists(path))
    {
        Response.Write("<hr/>" + folder + "<hr/>");

        string[] assemblyFolders = Directory.GetDirectories(path);
        foreach (string assemblyFolder in assemblyFolders)
        {
            Response.Write(assemblyFolder + "<br/>");
        }
    }
}

See this other answer here on stack overflow . 在堆栈溢出中查看此其他答案 I used this to enumerate the gac successfully. 我用它成功地枚举了gac。

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

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