简体   繁体   English

创建库.tar Framework 4.5和.Net Standard

[英]Create library targeting .Net Framework 4.5 and .Net Standard

I have a .NET library ( Products.SDK ) which i need to make it compatible with both .NET Framework 4.5 and .NET Standard 1.4. 我有一个.NET库( Products.SDK ),我需要使它与.NET Framework 4.5和.NET Standard 1.4兼容。

(later on i will also create a NuGet package out of this library) (稍后我将在这个库中创建一个NuGet包)

My question is: 我的问题是:

How do i write the code in this library? 我如何在这个库中编写代码? Considering that both frameworks are using different libraries / dependencies? 考虑到两个框架都使用不同的库/依赖项?

  1. Will i have two separate projects under the same solution? 我会在同一解决方案下有两个独立的项目吗?

    Products.SDK.sln Products.SDK.sln

    • Products.SDK.NetFramework Products.SDK.NetFramework
    • Products.SDK.NetStandard Products.SDK.NetStandard
  2. Will i have only one project, and use #if preprocessor directives do define different classes inside the library? 我只有一个项目,使用#if预处理器指令在库中定义不同的类吗?

     namespace Products.SDK { #if NETSTANDARD1_3 public class ProductsApi { public string ApiUrl { get; set; } } #else public class ProductsApi { public string ApiUrl { get; set; } } #endif } 

If option #1 is correct, how do i make sure that both projects gets compiled using the same Name / Namespace ( Products.SDK ) 如果选项#1正确,我如何确保使用相同的名称/命名空间( Products.SDK )编译这两个项目

I do think that option #2 is better, but i am not sure this is the correct way. 我认为选项#2更好,但我不确定这是正确的方法。

PS: PS:

I specify the target frameworks in the .csproj file 我在.csproj文件中指定了目标框架

  <PropertyGroup>
    <TargetFrameworks>netstandard1.4;net45</TargetFrameworks>
  </PropertyGroup>

You use option two - but only use #if in places where you really need it. 你使用选项二 - 但只在你真正需要它的地方使用#if The example you gave had the same code in both branches. 您给出的示例在两个分支中都具有相同的代码。

My Noda Time project takes exactly this approach, and we've had very few problems due to that. 我的Noda Time项目采用了这种方法,因此我们遇到的问题非常少。

Where possible, make the public API of your code the same for both platforms, with only implementation details differing. 尽可能使两个平台的代码的公共API相同,只有实现细节不同。 In particular, if you do need to expose some parts of your API only on one platform, I'd hope that it would be only for net45 . 特别是,如果您确实需要在一个平台上公开API的某些部分,我希望它只适用于net45 If you end up with something like this: 如果你最终得到这样的东西:

  • public class A - only on net45 公共等级A - 仅限于net45
  • public class B - only on netstandard 公共B级 - 仅限于netstandard
  • public class C - both 公共C级 - 两者兼而有之

then you're going to end up with significant problems later on, if you have multiple other projects depending on this. 如果你有多个其他项目,那么你最后会遇到重大问题。 Suppose project X targets netstandard1.4 and uses B, then project Y targets net45 and uses A - then an application Z running on .NET 4.7 and depending on both X and Y is going to have problems. 假设项目X的目标是netstandard1.4并使用B,那么项目Y的目标是net45并使用A - 然后是在.NET 4.7上运行的应用程序Z,并且根据X和Y都会出现问题。

It's definitely worth trying to avoid conditional code altogether. 绝对值得尝试完全避免条件代码。 In many cases you may find that while there's a slightly simpler piece of code that will work on just net45, there's still code that will work on both netstandard and net45. 在许多情况下,您可能会发现虽然只有一些稍微简单的代码可以在net45上运行,但仍有代码可以在netstandard net45上运行。 (Reflection springs to mind, where you'll want to use TypeInfo on netstandard, in places where you could just use Type in net45. You can use TypeInfo in both though.) (想到反射,你可能想在netstandard上使用TypeInfo ,在你可以使用Type in net45的地方。虽然你可以在两者中使用TypeInfo 。)

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

相关问题 安装Framework 4.5时,以.NET Framework 4为目标 - Targeting .NET Framework 4 when Framework 4.5 is installed 安装 .net Framework 4.5 定位公园 - install .net Framework 4.5 targeting park 如何从Visual Studio 2017中的.NET Framework 4.5控制台应用程序引用.NET标准库? - How Do You Reference a .NET Standard Library from a .NET Framework 4.5 Console Application in Visual Studio 2017? .Net定位框架4.5仅看到4.0参考? - .Net targeting framework 4.5 only sees 4.0 references? 在面向 .NET Framework 的 .NET 标准项目中未加载调试符号 - Debugging symbols not loading in .NET standard project targeting .NET Framework 如何将 .Net Standard Nuget 包添加到 .Net 4.5 Framework 项目中? - How to add .Net Standard Nuget package into .Net 4.5 Framework Project? .NET 4.5命名空间&#39;标准&#39; - .NET 4.5 namespace 'Standard' 在一个Nuget包中定位.Net核心框架和完整的.Net 4.5 / 4.6框架 - Targeting .Net Core Framework and Full .Net 4.5/4.6 Framework in one Nuget Package .net 标准库在 .net 核心中失败,但在框架中失败 - .net standard library fails in .net core but not in framework 如何在面向.net标准1.4的库中使用MetadataType属性 - How to use The MetadataType Attribute in a library targeting .net standard 1.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM