简体   繁体   English

如何构建两个二进制.NET Core 2控制台应用程序

[英]How to build two binaries .net core 2 console app

I am new in .Net core and c sharp. 我是.NET Core和C Sharp的新手。 i would like to build two binaries in the same namespace. 我想在同一个命名空间中建立两个二进制文件。 It's two console apps, one server and one client. 它是两个控制台应用程序,一台服务器和一个客户端。 I'd like to share some objects like "Scores" and think like that. 我想分享一些像“分数”的对象,然后这样思考。 Actually I have created two apps (via the "dotnet new console" command) but i can change and start again. 实际上,我已经创建了两个应用程序(通过“ dotnet新控制台”命令),但是我可以更改并重新启动。 I am working inside a docker container with a simple text editor (emacs). 我正在使用简单的文本编辑器(emacs)在docker容器中工作。 No visual studio or visual studio code there :) 那里没有Visual Studio或Visual Studio代码:)

It is possible or not ? 有没有可能?

edit : Okay, I will try to be more precise. 编辑 :好的,我会尝试更精确。 I have to create two programs: one is a server and the other is client. 我必须创建两个程序:一个是服务器,另一个是客户端。 Clients have to be connected to play a game (multiplayer). 必须连接客户端才能玩游戏(多人游戏)。 Does I have to create two distinct project (with their own namespaces/types/tests) or I can a simple project (just call "dotnet new console" command once) and share a same namespace, share some types between the server and the client (like a score class) and tests ? 我是否必须创建两个不同的项目(具有自己的名称空间/类型/测试),还是可以创建一个简单的项目(只需一次调用“ dotnet new console”命令)并共享相同的名称空间,在服务器和客户端之间共享某些类型(如分数班)和测试? here is my curent directory (client and server are two directories created via the "donet new console" command : 这是我的当前目录(客户端和服务器是通过“ donet new console”命令创建的两个目录:

├── README.md
├── client
│   ├── client.csproj
│   ├── obj
│   │   ├── client.csproj.nuget.cache
│   │   ├── client.csproj.nuget.g.props
│   │   ├── client.csproj.nuget.g.targets
│   │   └── project.assets.json
│   └── srcs
│       ├── Program.cs
│       └── Types
│           └── user.cs
├── docs
│   ├── rfc
│   │   └── donotremove
│   ├── text
│   │   └── donotremove
│   └── uml
│       └── donotremove
├── server
│   ├── obj
│   │   ├── project.assets.json
│   │   ├── server.csproj.nuget.cache
│   │   ├── server.csproj.nuget.g.props
│   │   └── server.csproj.nuget.g.targets
│   ├── server.csproj
│   └── srcs
│       ├── Program.cs
│       └── Types
│           └── server.cs
└── tests
    └── tests.cs

If I understood the question correctly, all u have to do is to change the namespace on every class. 如果我正确理解了这个问题,那么您要做的就是更改每个类的名称空间。

For example, lets say that one of your binary is called Yourappname.Server and other is called Yourappname.Client . 例如,假设您的二进制文件之一称为Yourappname.Server ,另一个称为Yourappname.Client And if we assume that the common namespace that u need to share is Yourcompany.Yourapp . 而且,如果我们假设您需要共享的通用命名空间是Yourcompany.Yourapp Then simply change the namespace of your classes to Yourcompany.Yourapp .It will do the work. 然后只需将类的命名空间更改为Yourcompany.Yourapp完成工作。

One more tip..you even can import classes in other namespaces using import statement using the using statement or can use the fully-qualified class name with the namespace. 还有一个技巧。您甚至可以使用using语句使用import语句在其他命名空间中导入类,或者可以将完全限定的类名与命名空间一起使用。

using Yourcompany.Yourapp;

or 要么

Yourcompany.Yourapp.Score score =new Yourcompany.Yourapp.Score();

Anyhow in both of the scenarios, you need to add references to binaries.You can do that using the .csproj file. 无论如何,在这两种情况下,都需要添加对二进制文件的引用。您可以使用.csproj文件来实现。

<ItemGroup>
    <ProjectReference Iclude="path to .csproj" />
</ItemGroup>

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

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