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

标签 c# .net win-universal-app windows-10-universal

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

  • 一个普通的 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/

相关文章:

c# - 两个私有(private)子类到一个公共(public)类

c# - 程序集加载失败,没有融合日志错误

.net - 单击时如何使 WPF 列表框子数据模板控件选择 ListBoxItem 容器?

windows-phone-8.1 - 使用 Windows 通用应用程序在设备之间同步数据

c# - Ajax.BeginForm(), OnSuccess - 获取事件目标

c# 将二进制数据读取到字符串中

具有延迟事件通知的 C# 作业队列实现

.net - 如何使 Array.Contains 对字符串数组不区分大小写?

c# - 深色/浅色主题 Assets 限定符

javascript - 在 Windows 托管 Web 应用程序中加载本地资源(威斯敏斯特元素)