C#:自定义程序集目录

发布于 2024-08-08 12:30:13 字数 221 浏览 2 评论 0 原文

假设我们有一个由 1 个可执行文件和 5 个库组成的应用程序。通常,所有这些都将包含在一个目录中,并且将从该目录加载库。

是否可以这样做,例如,我可以将一些库放在一个名为 Lib 的目录中,而将其余库放在一个名为 Lib2 的目录中?这样应用程序目录将只包含可执行文件本身,其他程序集将包含在各种逻辑目录中。

我该怎么做?我想知道如何加载程序集,以及如何使应用程序的构建将程序集放在正确的目录中。

Say we have an application which consists of one executable and 5 libraries. Regularly all of these will be contained in one directory and the libraries will be loaded from there.

Is it possible to do so that I can have for example some of the libraries in one directory called Lib, and the rest in one called Lib2? So that the application directory would only contain the executable itself and the other assemblies would be contained in various logical directories.

How can I do this? And I would like to know how to do the loading of the assemblies, but also how to make the building of the application put the assemblies in the right directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不美如何 2024-08-15 12:30:13

您可以向 app.config 添加其他搜索路径,以供其查找以加载程序集。例如,

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="lib;thirdParty" />
  </assemblyBinding>
</runtime>

您可以在此处。

You can add additional search paths to your app.config that it looks in to load assemblies. For example

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="lib;thirdParty" />
  </assemblyBinding>
</runtime>

You can see more details here.

北风几吹夏 2024-08-15 12:30:13

如果您想手动管理从何处获取汇编,您有两种可能性:

  1. 处理AppDomain.AssemblyResolve 事件(如 SLAks 所描述)。这是代码片段:

    static void Main(string[] args)
    {
       ...
       AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
       ...
    }
    
    静态 System.Reflection.Assembly CurrentDomain_AssemblyResolve(对象发送者,ResolveEventArgs args)
    {
       字符串 assemblyDir = "your_directory";
       程序集 asm = Assembly.LoadFrom(Path.Combine(assembliesDir, args.Name + ".dll"));
       返回汇编;
    }
    
  2. 使用您自己的设置创建域(通过设置 ApplicationBasePrivateBinPath AppDomainSetup 对象的属性)。

因此,如果您想继续在当前域中工作,则必须使用方法 1。

If you want to manually manage where to get assembles from, you have two possibilities:

  1. Handle the AppDomain.AssemblyResolve event (as it is described by SLaks). Here is the code snippet:

    static void Main(string[] args)
    {
       ...
       AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
       ...
    }
    
    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
       string assembliesDir = "your_directory";
       Assembly asm = Assembly.LoadFrom(Path.Combine(assembliesDir, args.Name + ".dll"));
       return asm;
    }
    
  2. Create a domain with your own settings (by setting ApplicationBase or PrivateBinPath properties of the AppDomainSetup object).

So if you want to continue working in current domain you have to use method 1.

半窗疏影 2024-08-15 12:30:13

要自动将程序集复制到您希望它们所在的文件夹,您可以将所有引用的“Copy Local”设置为 true,并执行构建后步骤将它们移动到子目录中。

或者,您可以将“Copy Local”设置为 false,并将引用的 DLL 文件添加为项目中相应子目录中的文件,并将“Build Action”设置为“复制到输出目录”(这将保留子目录)

让运行时找到它们的最灵活方法是处理 AppDomain.AssemblyResolve 事件并使用 Assembly.LoadFile。您需要某种方式让您的代码知道哪些程序集位于哪些目录中。

To make automatically copy the assemblies to the folder you want them to be in, you could set Copy Local to true for all of the references, and make a post-build step to move them into subdirectories.

Alternatively, you could set Copy Local to false, and add the referenced DLL files as files in the project in the appropriate subdirectories, and set Build Action to Copy to output directory (This will preserve subdirectories)

The most flexible way to make the runtime find them is to handle the AppDomain.AssemblyResolve event and manually load the assembly using Assembly.LoadFile. You would need some way for your code to know which assemblies are in which directories.

无人接听 2024-08-15 12:30:13

在您的评论中,您提到在运行时添加路径和位置。是的,您可以,但是您必须使用 Assembly.Load(),并且可能还需要使用反射。

In your comment you mention adding paths and locations at runtime. Yes you can, but you have to use Assembly.Load(), and possibly reflection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文