简体   繁体   English

如何在c#中保持项目之间共享的公共代码?

[英]How do I keep common code shared between projects in c#?

I'm kinda new to c# and I'm trying to figure out a good solution for keeping common code somewhere that is used by several projects. 我是c#的新手,我正在试图找到一个很好的解决方案,以便将公共代码保存在几个项目所使用的某个地方。

First I tried keeping it in a .cs file and add it to the projects that needs the code, but this COPIES the file meaning if I do any changes to the common code it will not be reflected in the various projects using this code. 首先,我尝试将其保存在.cs文件中并将其添加到需要代码的项目中,但这会复制文件,这意味着如果我对公共代码进行任何更改,它将不会反映在使用此代码的各种项目中。

Then I tried creating a class library instead and reference the DLL within the projects using the code. 然后我尝试创建一个类库,并使用代码在项目中引用DLL。 First of all I can't get this to work even though I am following the tutorials, Even when I reference the DLL I still cannot use it somehow, but even if this worked it still needs an extra DLL to be bundled with my app for some common code and that is not what I want. 首先,即使我遵循教程,我也无法让它工作,即使我引用DLL我仍然无法以某种方式使用它,但即使这样工作它仍然需要一个额外的DLL与我的应用程序捆绑在一起一些常见的代码,这不是我想要的。

Is there any way to keep common code that is REFERENCED without needing to ship a DLL w 有没有办法保持公共代码是REFERENCED而无需发送DLL w

The best solution is to have a common library that you reference in each of your projects. 最好的解决方案是在每个项目中引用一个公共库。 However you say you don't want to do that as it requires bundling additional dlls, is that really that big a issue? 但是你说你不想这样做,因为它需要捆绑额外的dll,这真的是一个大问题吗? You also mention that you can't get that to work, post the details of errors etc and we will be able to help. 你还提到你不能让它工作,发布错误的细节等,我们将能够提供帮助。

Another option, assuming you are using Visual Studio is to link the common file into your project rather than add it. 假设您使用Visual Studio,另一个选项是将公共文件链接到您的项目而不是添加它。 This will keep the common file in its current location and not move it into your project directory. 这将使公共文件保持在其当前位置,而不是将其移动到项目目录中。

To do this, go to the add existing file dialog but on the 'Add' button click the down arrow and select 'Link' instead. 要执行此操作,请转到添加现有文件对话框,但在“添加”按钮上单击向下箭头并选择“链接”。

As mentioned above using a common library is the best solution so if bundling this is a unsurmountable issue you could look into using ilmerge. 如上所述,使用通用库是最佳解决方案,因此如果捆绑这是一个不可逾越的问题,您可以考虑使用ilmerge。 This will merge all your dlls/exes into a single file that you can then deploy. 这会将您的所有dll / exes合并到一个文件中,然后您可以部署该文件。

You can add a link to a common file: 您可以添加指向公共文件的链接:

  1. Right click on the project 右键单击该项目
  2. Select 'Add' => 'Existing Item...' 选择“添加”=>“现有项目...”
  3. Find the .cs file you want to share 找到您要共享的.cs文件
  4. Click the drop down on the Add box 单击“添加”框上的下拉列表
  5. Choose, Add as Link. 选择,添加为链接。

Hope this helps! 希望这可以帮助!

You were almost there. 你快到了。 You can add your existing file, but note that you can expand the "add" button and choose to link to it instead. 您可以添加现有文件,但请注意,您可以展开“添加”按钮并选择链接到该文件。

You can add a linked file to a project - this does not create a copy but simply references it. 您可以将链接文件添加到项目中 - 这不会创建副本,而只是引用它。

See the "Adding an Existing Item as a Link" section in the linked page: 请参阅链接页面中的“将现有项目添加为链接”部分:

To create a link to an existing item: 要创建指向现有项目的链接:

  1. In Solution Explorer, select the target project. 在Solution Explorer中,选择目标项目。
  2. On the Project menu, select Add Existing Item. 在“项目”菜单上,选择“添加现有项”。
  3. In the Add Existing Item dialog box, locate and select the project item you want to link. 在“添加现有项”对话框中,找到并选择要链接的项目项。
  4. From the Open button drop-down list, select Add As Link. 从“打开”按钮下拉列表中,选择“添加为链接”。

In visual studio you can add a link to an existing item: 在visual studio中,您可以添加指向现有项目的链接:

In the solution explorer, right click and choose Add -> Existing Item... Browse to the file and instead of clicking Add, choose Add As Link. 在解决方案资源管理器中,右键单击并选择添加 - >现有项目...浏览到该文件,而不是单击添加,选择添加为链接。

But I would advise to use a separate project for common code. 但我建议使用一个单独的项目来使用通用代码。

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

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