在多个项目之间共享 C# 类源代码

发布于 2024-08-29 01:42:33 字数 271 浏览 6 评论 0原文

我编写了一个类来处理我的应用程序中的内部日志记录。 现在我想在另一个新的、完全独立的项目中使用这个类。

我可以简单地将文件复制到新的项目文件夹,但我只想维护它的一份副本,以便随着时间的推移,其中的所有更改都将应用到两个项目。

我可以使用“添加现有文件”,但是我应该将文件放在哪里,以便下一个开发人员知道它是必需的。我曾经有一个“共享”文件夹,但有一次该文件夹没有被带入下一台开发计算机。

组织这个的最佳方式是什么,以便它对新维护人员最有意义,并最大限度地减少项目中断开链接的风险。

I have written a class that will handle internal logging in my application.
Now I want to use this class in another new and totally separate project.

I could simply copy the file to the new project folder, but I would like to only have one copy of it to maintain so that all changes in it will apply to both projects over time.

I can use the "add existing file", but where do I put the file so that the next developer knows that it is required. I have once had a "shared" folder for this but one time that folder was not brought into the next development computer.

What is the best way to organize this so that it makes most sense for new maintainers and minimizes the risk for broken links in projects.

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

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

发布评论

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

评论(7

猫瑾少女 2024-09-05 01:42:33

您可以创建一个具有此类的库项目,这样您所要做的就是添加对该项目的引用。

如果没有这个选项,您可以使用“右键单击 -> 添加现有项目 -> 添加为链接”,这样您只有一份代码副本,但它可以存在于多个项目中。

You could create a library project that has this class this way all you have to do is add a reference to that project.

If that is no option you could use "Right click -> add existing item -> Add as link" this way you only have one copy of the code but it can exist in multiple projects.

ㄖ落Θ余辉 2024-09-05 01:42:33

共享类库是最好的解决方案。

但是您可以将文件作为链接添加到 VS 项目,而不是复制。为此,请使用添加现有项目对话框的添加按钮上的下拉菜单。

A class library to share is the best solution.

But you can add a file to a VS project as a link rather than copying. To do this use the drop down on the Add button of the add existing item dialogue.

温暖的光 2024-09-05 01:42:33

如果您确实需要共享源代码,例如,如果您同时拥有同一程序集的 32 位和 64 位版本(如果您依赖本机代码,有时需要这样做),您可以使用给出的答案在此线程中:如何在之间共享代码Visual Studio 中的项目/解决方案?

(查找“两个项目之间的“链接”代码文件”。)

If you actually need to share the source code, for instance if you have a simultaneous 32-bit and a 64-bit build of the same assembly (which is some times required if you depend on native code), you can use the answer given in this thread: How do you share code between projects/solutions in Visual Studio?

(Look for '"link" a code file between two projects'.)

悲喜皆因你 2024-09-05 01:42:33

将您的日志记录代码分解为单独的程序集。

然后,您可以将该程序集包含在应使用该记录器的项目中。

它使一切都保持良好和干净,您只需维护一组代码,而不必担心每个副本。

Break your logging code to a seperate assembly.

You can then include that assembly in the projects that should use that logger.

It keeps everything nice and clean and you'll only have to maintain one set of code rather than having to worry about each copy.

旧人哭 2024-09-05 01:42:33

创建一个包含此类的新程序集。

然后您的所有其他项目都可以引用该程序集并使用其中的类。

Create a new assembly that contains this class.

Then all your other projects can refer to this assembly and use the class within.

滥情稳全场 2024-09-05 01:42:33

将类放入唯一的命名空间中,然后将该文件作为链接添加到您需要使用它的所有项目。确保为将使用该类的唯一名称空间放置一个 using 语句,否则您必须完全限定它的每次使用。
对于这样的情况,我认为这比汇编要好得多,因为它不会生成不必要的交叉汇编引用,对于您自己的代码的一部分,您只是希望在项目之间共享。

Put the class in a unique namespace, then add the file as a link to all projects you need to use it. Make sure you place a using statement for that unique namespace where you will be using the class, or you will have to fully qualify every use of it.
For situations like this, I think this is much preferable than an assemply, since it does not generate needless cross-assemply references, for something that is part of your own code that you just wish to share between your projects.

情话难免假 2024-09-05 01:42:33

您还可以使用版本控制系统来执行此操作。

我们通常会创建包含解决方案和主项目的目录,然后将该目录提交到 SVN。然后所有共享库都使用 svn:external 链接 - 因此所有项目都存在于同一解决方案目录中。

提交后,任何人都可以检查具有所有外部依赖项的工作解决方案。

You can also use use your versioning system to do this.

We usually create Directory with Solution and Main Project and then commit this directory to SVN. Then all shared libraries are linked using svn:external - so all Projects exists in same solution directory.

After commit anyone can checkout working solution with all external dependencies.

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