gpt4 book ai didi

c# - 如何避免普通库和通用库之间的重复?

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:55 25 4
gpt4 key购买 nike

我有一个由两部分组成的解决方案:

  • 一个普通的 Windows 库(针对任何 CPU Windows 编译)
  • 通用库(针对 Windows 10 平台编译)

这 2 个项目共享一组配置值,例如,因为它们通过套接字通信,端点的端口号。所以我最终这样做了:

// In Proj1/service.cs
namespace Proj.Lib {
public class Service {
private string port = "6662";
}
}

// In Proj2/service.cs
namespace Proj.UniversalApp {
public class Service {
private string port = "6662";
}
}

这两个项目基本上是针对不同平台编译的,因此我不能将一个项目引用到另一个项目中,以避免在 port 上出现冗余。

对我来说,理想的解决方案是创建一个包含公共(public)变量的公共(public)项目,并在 Proj1 (Proj.Lib) 和 Proj2 中引用它(Proj.UniversalApp)。

如何?

最佳答案

创建一个可移植类库 (PCL) 并让它针对您的项目所针对的两个平台。然后您将能够引用公共(public)项目/库。

Cross-Platform Development with the Portable Class Library

The .NET Framework Portable Class Library project type in Visual Studio helps you build cross-platform apps and libraries for Microsoft platforms quickly and easily.

Portable class libraries can help you reduce the time and costs of developing and testing code. Use this project type to write and build portable .NET Framework assemblies, and then reference those assemblies from apps that target multiple platforms such as Windows and Windows Phone.

Even after you create a Portable Class Library project in Visual Studio and start developing it, you can change the target platforms. Visual Studio will compile your library with the new assemblies, which helps you identify the changes you need to make in your code.

关于c# - 如何避免普通库和通用库之间的重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38686885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com