.net 你如何在 Visual Studio 中的项目/解决方案之间共享代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1116465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:00:07  来源:igfitidea点击:

How do you share code between projects/solutions in Visual Studio?

.netvisual-studiocode-sharing

提问by pupeno

I have two solutions which have some common code, so I'd like to extract it out and share it between them. Furthermore, I'd like to be able to release that library independently because it might be useful to others.

我有两个具有一些通用代码的解决方案,因此我想将其提取出来并在它们之间共享。此外,我希望能够独立发布该库,因为它可能对其他人有用。

  • What's the best way to do it with Visual Studio 2008?
  • Is a project present in more than one solution?
  • Do I have a separate solution for the separate piece of code?
  • Can a solution depend on another one?
  • 使用 Visual Studio 2008 执行此操作的最佳方法是什么?
  • 一个项目是否存在于多个解决方案中?
  • 对于单独的一段代码,我是否有单独的解决方案?
  • 一个解决方案可以依赖另一个解决方案吗?

采纳答案by ilivewithian

A project can be referenced by multiple solutions.

一个项目可以被多个解决方案引用。

Put your library or core code into one project, then reference that project in both solutions.

将您的库或核心代码放入一个项目中,然后在两个解决方案中引用该项目。

回答by Andomar

You can "link" a code file between two projects. Right click your project, choose Add-> Existing item, and then click the down arrow next to the Addbutton:

您可以在两个项目之间“链接”一个代码文件。右键单击您的项目,选择Add-> Existing item,然后单击Add按钮旁边的向下箭头:

Screengrab

截屏

In my experience linking is simpler than creating a library. Linked code results in a single executable with a single version.

根据我的经验,链接比创建库更简单。链接的代码产生具有单一版本的单一可执行文件。

回答by Aseem Kishore

File > Add > Existing Project...will let you add projects to your current solution. Just adding this since none of the above posts point that out. This lets you include the same project in multiple solutions.

File > Add > Existing Project...将让您将项目添加到当前的解决方案中。只是添加这个,因为上面的帖子都没有指出这一点。这使您可以在多个解决方案中包含相同的项目。

回答by Jon Skeet

You caninclude a project in more than one solution. I don't think a project has a concept of which solution it's part of. However, another alternative is to make the first solution build to some well-known place, and reference the compiled binaries. This has the disadvantage that you'll need to do a bit of work if you want to reference different versions based on whether you're building in release or debug configurations.

可以在多个解决方案中包含一个项目。我认为一个项目没有关于它属于哪个解决方案的概念。但是,另一种选择是将第一个解决方案构建到某个众所周知的地方,并引用已编译的二进制文件。这样做的缺点是,如果您想根据是在发布配置还是调试配置中构建来引用不同的版本,则需要做一些工作。

I don't believe you can make one solution actually depend on another, but you can perform your automated builds in an appropriate order via custom scripts. Basically treat your common library as if it were another third party dependency like NUnit etc.

我不相信您可以使一个解决方案实际上依赖于另一个解决方案,但是您可以通过自定义脚本以适当的顺序执行自动构建。基本上将您的公共库视为另一个第三方依赖项,如 NUnit 等。

回答by Ruben Bartelink

You can wild-card inline using the following technique (which is the way in which @Andomar's solution is saved in the .csproj)

您可以使用以下技术内联通配符(这是@Andomar 的解决方案保存在 .csproj 中的方式)

<Compile Include="..\MySisterProject\**\*.cs">
  <Link>_Inlined\MySisterProject\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

Put in:

投放:

    <Visible>false</Visible>

If you want to hide the files and/or prevent the wild-card include being expanded if you add or remove an item from a 'virtual existing item' folder like MySisterProjectabove.

如果您想隐藏文件和/或防止在“虚拟现有项目”文件夹中添加或删除项目时扩展通配符包含,MySisterProject如上。

回答by John Saunders

You would simply create a separate Class Library project to contain the common code. It need not be part of any solution that uses it. Reference the class library from any project that needs it.

您只需创建一个单独的类库项目来包含公共代码。它不必是使用它的任何解决方案的一部分。从任何需要它的项目中引用类库。

The only trick at all is that you will need to use a file reference to reference the project, since it will not be part of the solutions that refer to it. This means that the actual output assembly will have to be placed in a location that can be accessed by anyone building a project that references it. This can be done by placing the assembly on a share, for instance.

唯一的技巧是您需要使用文件引用来引用项目,因为它不会成为引用它的解决方案的一部分。这意味着实际的输出程序集必须放置在任何构建引用它的项目的人都可以访问的位置。例如,这可以通过将程序集放置在共享上来完成。

回答by Philippe Leybaert

You could include the same project in more than one solution, but you're guaranteed to run into problems sometime down the road (relative paths can become invalid when you move directories around for example)

您可以在多个解决方案中包含同一个项目,但您肯定会在某个时候遇到问题(例如,当您移动目录时,相对路径可能会变得无效)

After years of struggling with this, I finally came up with a workable solution, but it requires you to use Subversion for source control (which is not a bad thing)

经过多年的努力,我终于想出了一个可行的解决方案,但它需要您使用Subversion进行源代码控制(这不是一件坏事)

At the directory level of your solution, add a svn:externalsproperty pointing to the projects you want to include in your solution. Subversion will pull the project from the repository and store it in a subfolder of your solution file. Your solution file can simply use relative paths to refer to your project.

在解决方案的目录级别,添加指向要包含在解决方案中的项目的svn:externals属性。Subversion 将从存储库中提取项目并将其存储在解决方案文件的子文件夹中。您的解决方案文件可以简单地使用相对路径来引用您的项目。

If I find some more time, I'll explain this in detail.

如果我找到更多时间,我会详细解释这一点。

回答by Mehmet Aras

Extract the common code into a class library project and add that class library project to your solutions. Then you can add a reference to the common code from other projects by adding a project reference to that class library. The advantage of having a project reference as opposed to a binary/assembly reference is that if you change your build configuration to debug, release, custom, etc, the common class library project will be built based on that configuration as well.

将公共代码提取到类库项目中,并将该类库项目添加到您的解决方案中。然后,您可以通过添加对该类库的项目引用来添加对来自其他项目的公共代码的引用。拥有项目引用而不是二进制/程序集引用的优点是,如果您将构建配置更改为调试、发布、自定义等,公共类库项目也将基于该配置构建。

回答by user88637

It is a good idea to create a dll class library that contain all common functionality. Each solution can reference this dll indepently regardless of other solutions.

创建一个包含所有常用功能的 dll 类库是个好主意。无论其他解决方案如何,每个解决方案都可以独立地引用此 dll。

Infact , this is how our sources are organized in my work (and I believe in many other places).

事实上,这就是我们在我的工作中组织资源的方式(我相信在许多其他地方)。

By the way , Solution can't explicitly depend on another solution.

顺便说一句,解决方案不能明确依赖另一个解决方案。

回答by Croko

Two main steps involved are

涉及的两个主要步骤是

1- Creating a C++ dll

1- 创建 C++ dll

In visual studio

在视觉工作室

New->Project->Class Library in c++ template. Name of project here is first_dll in 
visual studio 2010. Now declare your function as public in first_dll.h file and 
write the code in first_dll.cpp file as shown below.

Header File code

头文件代码

// first_dll.h

using namespace System;

namespace first_dll 
{

public ref class Class1
{
public:
    static double sum(int ,int );
    // TODO: Add your methods for this class here.
};
}

Cpp File

Cpp文件

//first_dll.cpp
#include "stdafx.h"

#include "first_dll.h"

namespace first_dll
{

    double Class1:: sum(int x,int y)
    {
        return x+y;
    }

 }

Check This

检查这个

**Project-> Properties -> Configuration/General -> Configuration Type** 

this option should be Dynamic Library(.dll)and build the solution/project now.

此选项应为动态库 (.dll)并立即构建解决方案/项目。

first_dll.dllfile is created in Debug folder

Debug 文件夹中创建first_dll.dll文件

2- Linking it in C# project

2- 在 C# 项目中链接它

Open C# project

打开 C# 项目

Rightclick on project name in solution explorer -> Add -> References -> Browse to path 
where first_dll.dll is created and add the file.

Add this line at top in C# project

在 C# 项目的顶部添加这一行

Using first_dll; 

Now function from dll can be accessed using below statement in some function

现在可以在某些函数中使用以下语句访问 dll 中的函数

double var = Class1.sum(4,5);

I created dll in c++ project in VS2010 and used it in VS2013 C# project.It works well.

我在 VS2010 的 c++ 项目中创建了 dll,并在 VS2013 C# 项目中使用它。它运行良好。