简体   繁体   English

如何在同一个解决方案中将一个项目的功能包含到另一个项目中?

[英]How to include functionality from one project into another in the same solution?

In my company, we are creating C# client/server applications as follows:在我的公司,我们正在创建 C# 客户端/服务器应用程序,如下所示:
We create three project inside one single Visual Studio solution:我们在一个 Visual Studio 解决方案中创建了三个项目:

  • Product.General产品.通用
  • Product.Client产品.客户
  • Product.Server产品.服务器

The "General" project contains functionality, to be used by both client and server parts. “General”项目包含可供客户端和服务器部分使用的功能。

In order to make this work, we compile the "Product.General" and add the binary as a reference to the "Product.Client" and "Product.Server" projects.为了完成这项工作,我们编译“Product.General”并将二进制文件添加为对“Product.Client”和“Product.Server”项目的引用。

In our source code, this looks as follows:在我们的源代码中,如下所示:

In the "General" project:在“通用”项目中:

namespace Product.Customer.Configuration
{
    public class SettingManager
    {
        ...
    }
}

In the "Server" project:在“服务器”项目中:

using Product.Customer.Configuration;
...
var settingManager = ...<SettingManager>();

I don't like, because amongst other first you need to get the "General" part compiled before you can even start working on your "Client" or "Server" project.我不喜欢,因为除此之外,您首先需要编译“常规”部分,然后才能开始处理“客户端”或“服务器”项目。

How can I get such a system to work, without needing to add compiled binaries into projects' references?我怎样才能让这样的系统工作,而不需要将编译的二进制文件添加到项目的引用中?
Thanks in advance提前致谢

You should add the reference with Add Project Reference and then select the General Project like this.您应该使用 Add Project Reference 添加引用,然后像这样添加通用项目 select。 and whenever you are making changes just do Ctrl+Shift+B to build the solution.每当您进行更改时,只需执行 Ctrl+Shift+B 即可构建解决方案。

在此处输入图像描述

在此处输入图像描述

If you're working with Visual Studio, right click on the References menu in the Product.Client or Product.Server project, click Add Reference in the popup select the Projects tab and then add a checkmark where needed (in your case Product.General ) (see vivek nuna's answer ).如果您正在使用 Visual Studio,请右键单击Product.ClientProduct.Server项目中的References菜单,单击Projects选项卡弹出 select 中的Add Reference ,然后在需要的地方添加复选标记(在您的情况下为Product.General )(见vivek nuna的回答)。 I assume it's basically the same in something liked Rider.我认为它在类似 Rider 的东西中基本相同。

If you're not using Visual Studio, but are using the new .NET ( not .NET Framework) you can use the dotnet cli tool like so如果您不使用 Visual Studio,而是使用新的 .NET(不是.NET 框架),您可以像这样使用dotnet cli 工具

dotnet add path/to/Product.Client.csproj reference path/to/Product.General.csproj

If the working directory of the shell your using contains a.csproj file, you can omit the first path/to/*.csproj , so doing something like this should work如果您使用的 shell 的工作目录包含一个 .csproj 文件,您可以省略第一个path/to/*.csproj ,所以这样做应该可以

cd Product.Client
dotnet add ../Product.General/Product.General.csproj

If you're not using Visual Studio or some other IDE and for some reason can't use the dotnet cli tool, then you can manually edit the.csproj files, like so如果您没有使用 Visual Studio 或其他一些 IDE 并且由于某种原因无法使用 dotnet cli 工具,那么您可以手动编辑 .csproj 文件,如下所示

<ItemGroup>
  <ProjectReference Include="path/to/Product.General.csproj" />
</ItemGroup>

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

相关问题 将服务引用从一个项目复制到同一解决方案中的另一个项目 - Copy service reference from one project to another project in same solution 在同一解决方案中从另一个项目中的一个项目打开xaml页面 - Open a xaml page from one project in another project in the same solution 在同一解决方案中将一个项目的照片文件用于另一个项目 - Use photo file from one project to another project in the same solution 一个项目的class在同一个方案下如何在另一个项目中使用? - How to use a class of one project in another project under the same solution? 将用户控件从一个项目复制到同一解决方案中的另一个项目 - Copy User Control from one project to another within same solution 在同一解决方案中使用从一个项目到另一个项目的依赖关系 - Use dependency from one project to another in the same solution 如何从同一解决方案检查另一个项目的版本 - How to check the version of another project from the same solution 如何从相同解决方案的另一个项目中加载文件 - How to Load a file from another project of same solution 如何从同一个解决方案中读取另一个项目的配置值? - How to read the config value of another project from the same solution? 绑定到同一解决方案中另一个项目的属性 - Bind to property from another project in same solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM