将 C# 项目引用放置在子文件夹中

发布于 2024-10-17 03:32:01 字数 335 浏览 3 评论 0原文

我有一个包含多个项目的解决方案,所有项目都输出 dll(当然主应用程序除外)。对于所有引用,复制本地设置为 true,一切都很好,dll 与 exe 位于同一目录中。

我的问题是这很丑。我想将所有 dll 放在一个子文件夹中(准确地说,实际上是两个子文件夹)。如何在 Visual Studio 2008 中执行此操作?

我发现了一些看起来相似的问题,但我找不到我知道必须存在的简单答案。

编辑:更清楚地说,我想知道如何使程序集加载器在操作目录之外的其他地方查找引用。用户将与目录中的一些其他文件进行交互,对他们来说,混乱越少越好。

编辑 2:我还想避免使用 GAC。该应用程序需要是独立的。

I have a solution with multiple projects that all output dlls (except for the main application of course). Copy local is set to true for all of the references and everything is fine and dandy with dlls in the same directory as the exe.

My problem is that this is ugly. I'd like to put all the dll's in a subfolder (actually two subfolders down to be precise). How can I do this in Visual Studio 2008?

I've found a few questions that seem similar but I couldn't find the simple answer that I know has to exist.

EDIT: To be clearer, I want to know how to make the assembly loader look for references somewhere besides the operating directory. Users will be interacting with some of the other files in the directory, and the less clutter there is for them the better.

EDIT 2: I also want to avoid using the GAC. The application needs to be self contained.

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

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

发布评论

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

评论(5

审判长 2024-10-24 03:32:01

您尝试过 AppDomain 命名空间吗?

AppDomain.CurrentDomain.AppendPrivatePath

http://www.vcskicks.com/csharp_ assembly.php

Have you tried the AppDomain namespace?

AppDomain.CurrentDomain.AppendPrivatePath

http://www.vcskicks.com/csharp_assembly.php

白云悠悠 2024-10-24 03:32:01

或者AssemblyResolve

public static class AssemblyResolver { 
    static AssemblyResolver() { 
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender,  ResolveEventArgs args) {
            return Assembly.LoadFrom(...); 
        }); 
    }  
} 

Or AssemblyResolve

public static class AssemblyResolver { 
    static AssemblyResolver() { 
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender,  ResolveEventArgs args) {
            return Assembly.LoadFrom(...); 
        }); 
    }  
} 
知你几分 2024-10-24 03:32:01

使用 app.config 元素指示 .NET 运行时在子文件夹中查找以查找其他程序集。请参阅此处

Use the app.config <probing>element to instruct the .NET Runtime to look in the subfolders to locate additional assemblies. See here.

暮年慕年 2024-10-24 03:32:01

您不能将这些引用放入子文件夹中。因为它们不会被应用程序的运行时“看到”。

首先放置它们的位置是调试目录,然后是全局程序集缓存(又名 GAC)。请注意,您在“添加引用”对话框的 (.Net) 选项卡中看到的实际上是 GAC 目录中的引用。

注意:如果您使用 TFS 作为后端源代码管理,请注意,在执行签入时,引用不会复制到源代码控制存储库,而是必须手动复制它们。

You can't put those references in a subfolder. Since they will not be "seen" by your application's run-time.

The first place to put them is in your debug directory then in the Global Assembly Cache (aka GAC). Note that what you see in the (.Net) tab in Add Reference dialog is actually the references in the GAC directory.

Note: If you use TFS as a backend source control, take notice that reference are not copied to the source control repository when you perform a check-in, rather you have to copy them manually.

芸娘子的小脾气 2024-10-24 03:32:01

我刚刚发表了一篇文章,详细解释了所有这些。
通过 .NET 程序集和 Visual Studio 项目对代码库进行分区

以下是本文得出的指导原则:

  • 大幅减少代码库的程序集数量。
  • 仅当物理分离的特定要求证明合理时才创建新装配。
  • 在 Visual Studio 项目中,使用“按程序集引用”而不是“按 Visual Studio 项目引用”。
  • 切勿使用 Visual Studio 引用选项“Copy Local = True”。
  • 将所有 VS 解决方案和构建操作 .bat 文件放在 $rootDir$ 目录中。
  • 编译目录中的所有程序集: $rootDir$\bin\Debug 和 $rootDir$\bin\Release
  • 使用目录 $rootDir$\bin 托管测试程序集。

I just publish an article that explain all these with details.
Partitioning Your Code Base Through .NET Assemblies and Visual Studio Project

Here are the resulting guidelines of the article:

  • Reduce drastically the number of assemblies of your code base.
  • Create a new assembly only when this is justified by a specific requirement for physical separation.
  • In a Visual Studio project, use ‘reference by assembly’ instead of ‘reference by Visual Studio project’.
  • Never use the Visual Studio referencing option ‘Copy Local = True’.
  • Put all VS solutions and Build action .bat files in a $rootDir$ directory.
  • Compile all assemblies in directories: $rootDir$\bin\Debug and $rootDir$\bin\Release
  • Use the directory $rootDir$\bin to host tests assemblies.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文