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

How to avoid duplication between plain libraries and universal libraries?

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

这 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上的冗余。

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

如何?

创建一个可移植的 class 库 (PCL) 并让它针对您的项目所针对的两个平台。然后您将能够引用常见的 project/library.

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.