我可以告诉我的应用程序从特定位置加载所有程序集(DLL)吗? [英] Can I tell my application to load all assemblies (DLLs) from a specific location?

查看:73
本文介绍了我可以告诉我的应用程序从特定位置加载所有程序集(DLL)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用安装在装有我的应用程序的每台计算机上的第三方SDK,我不想在我的应用程序部署中添加这些SDK DLL,因为它占用大量空间.由于其他原因,不能在GAC中注册DLL.无论如何,有一种方法可以告诉我的应用程序所需的DLL在特定路径上可用吗?

解决方案

是的,可以在例如 Common Programs中部署共享文件(如果出于某种原因您不希望将它们放入GAC中). 文件夹.

根据您的应用程序的工作方式,您可能需要使用组装来显式加载它们.LoadFrom()使用您从用于 Environment.SpecialFolders.CommonPrograms AppDomain.AssemblyResolve 事件的附加事件处理程序./p>

让我们看一个简单的原始示例:

// Put this in your initialization code
public static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveAssembly(MyResolveEventHandler);
}

private static Assembly ResolveAssembly(object sender, ResolveEventArgs e)
{
    // Clean, check, double check and verify path name to be sure it's a valid
    // assembly name and it's not a relative path itself, you may even check e.RequestingAssembly
    string path = ...; 

    return Assembly.LoadFrom(path);
}

如果您拥有目录,并且想要在启动时全部加载(以防万一...):

private static LoadThemAll(folderPath)
{
    foreach (var path in Directory.GetFiles(folderPath, "*.dll"))
        Assembly.LoadFrom(path);
}

不要忘记添加适当的错误处理(对于非托管DLL,错误的版本以及可能发生的所有其他事情,尤其是因为程序集将被加载到您的AppDomain中,并且每个人都可能在该文件夹中放置了一个恶意程序).

当然,所有这些都可以应用到任何(可访问的)文件夹中,这是最好的...这取决于您要加载的内容及其部署方式(您甚至可以在Registry中搜索以动态搜索找到支持文件的位置.

最后,如果您引用的程序集是强签名的,并且它们位于已知的,固定的,不可变的位置,则可以使用 <codebase> .

My application uses a third party SDK which is installed on every machine that has my application, I do not want to add these SDK DLLs in my application deployment because it takes a lot of space. Registering the DLLs in the GAC is not an option for other reasons. Anyway is there a way to tell my application that the required DLLs are available on a specific path?

解决方案

Yes, shared files (if for some reason you do not want to put them in GAC) can be deployed, for example, in the Common Programs folder.

According to the way your application works you may need to load them explictly with Assembly.LoadFrom() using the path you get from Environment.GetSpecialFolder() for Environment.SpecialFolders.CommonPrograms or attaching and event handler for AppDomain.AssemblyResolve event.

Let's see a raw and simple example:

// Put this in your initialization code
public static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveAssembly(MyResolveEventHandler);
}

private static Assembly ResolveAssembly(object sender, ResolveEventArgs e)
{
    // Clean, check, double check and verify path name to be sure it's a valid
    // assembly name and it's not a relative path itself, you may even check e.RequestingAssembly
    string path = ...; 

    return Assembly.LoadFrom(path);
}

If you have the directory and you want to load them all at startup (just in case...):

private static LoadThemAll(folderPath)
{
    foreach (var path in Directory.GetFiles(folderPath, "*.dll"))
        Assembly.LoadFrom(path);
}

Do not forget to add proper error handling (for non managed DLLs, wrong versions and everything else may happen, especially because that assemblies will be loaded in your AppDomain and everyone may put a malicious one in that folder).

Of course all of these can be applied to any (accessible) folder, which one is the best...well it depends on what you're trying to load and how it's deployed (you can even search in Registry to dynamically find where support files are located).

Finally in case assemblies you referenced are strong signed and they reside on a known, fixed, immutable location you can use <codebase> in your .config file.

这篇关于我可以告诉我的应用程序从特定位置加载所有程序集(DLL)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆