简体   繁体   English

如何创建针对.NET 2.0和.NET Standard的库?

[英]How to create a library that targets both .NET 2.0 and .NET Standard?

I have a small library that currently supports .NET 2.0+. 我有一个目前支持.NET 2.0+的小型库

I don't use any features of the later framework versions so it would be nice to keep 2.0 support, but I also want to target .NET Core (or more precisely, .NET Standard). 我不使用更高版本框架的任何功能,因此保持2.0支持会很好,但是我也想针对.NET Core(或更准确地说,.NET Standard)。

I tried to add both frameworks to project.json : 我试图将两个框架都添加到project.json

"frameworks": {
  "net20": {},
  "netstandard1.6": {
    "imports": "dnxcore50"
  }
}

But the NuGet packages my library needs to run on .NET Standard ( System.Reflection and Microsoft.AspNetCore.WebUtilities ) aren't compatible with .NET 2.0. 但是我的库需要在.NET Standard( System.ReflectionMicrosoft.AspNetCore.WebUtilities )上运行的NuGet软件包与.NET 2.0不兼容。

How can I solve this issue without maintaining two completely separate projects with almost identical code? 如何在不使用几乎相同的代码维护两个完全独立的项目的情况下解决此问题?

You can't, if you depend on Microsoft.AspNetCore.* packages as the absolute minimum for supporting .NET Standard is .NET 4.5. 如果您依赖Microsoft.AspNetCore.*软件包,则不能Microsoft.AspNetCore.*因为支持.NET Standard的绝对最低要求是.NET 4.5。

.NET 4.5 is the first version to include System.Runtime on which .NET Core is based on. .NET 4.5是第一个包含System.Runtime版本,.NET Core基于该版本。 But when you think closer about it, it also makes no sense. 但是,当您仔细考虑时,这也没有任何意义。 If you need support for ASP.NET Core within your library. 如果您的库中需要对ASP.NET Core的支持。

If your library is supposed to run for ASP.NET Core and ASP.NET 4 (ie MVC 5, WebApi 2), then you will need to use your ASP.NET Dependencies conditionally and use #if preprocessor directives. 如果您的库应该为ASP.NET Core和ASP.NET 4(即MVC 5,WebApi 2)运行,那么您将需要有条件地使用ASP.NET依赖关系,并使用#if预处理程序指令。

"frameworks": {
  "net20": {
    "dependencies": {
      "NameOf.AspNetLegacyPackage": "1.2.3"
    }
  },
  "netstandard1.3": {
    "dependencies": {
      "Microsoft.AspNetCore.WebUtilities" : "1.1.0"
    },
    "imports": "dnxcore50"
  }
}

I used netstandard1.3 as that's the minimum for Microsoft.AspNetCore.WebUtilities , but depending on your other dependencies you may need to go higher or lower. 我使用netstandard1.3因为这是Microsoft.AspNetCore.WebUtilities的最低要求,但是根据您的其他依赖项,您可能需要提高或降低。

NameOf.AspNetLegacyPackage is the name of the package which contains the same functionality as Microsoft.AspNetCore.WebUtilities which you need, but which works on .NET Framework 2.0, if there is any. NameOf.AspNetLegacyPackage是软件包的名称,该软件包包含与所需的Microsoft.AspNetCore.WebUtilities相同的功能,但可以在.NET Framework 2.0上运行(如果有)。 If not you have to remove it and write the replacement functions yourself. 如果不是,则必须将其删除并自己编写替换功能。

Then in your code use 然后在你的代码中使用

#if NETSTANDARD1_3
    // Code or APIs which is only available in netstandard1.3/net4.6 
    // this includes the Microsoft.AspNetCore.WebUtillities
#else
    // Use code or API which runs under .NET Framework 2.0
#endif

Alternatively, if you are going to give up the .NET Framework 2.0 support and go for 4.5.1, you can keep using Microsoft.AspNetCore.WebUtillities (see NuGet page for dependencies) it in both 另外,如果您要放弃对.NET Framework 2.0的支持并转而使用4.5.1,则可以在两个版本中继续使用Microsoft.AspNetCore.WebUtillities (请参见NuGet页面以获取相关性)。

"dependencies": {
  "Microsoft.AspNetCore.WebUtilities" : "1.1.0"
},
"frameworks": {
  "net451": {
  },
  "netstandard1.3": {
    "imports": "dnxcore50"
  }
}

You can do this by putting your code into a Shared project, then created a Class Library which targets .Net 2.0, and another Class Library project which targets NetStandard. 您可以通过将代码放入共享项目中,然后创建一个针对.Net 2.0的类库以及另一个针对NetStandard的类库项目来实现。 If there are things in 2.0 or Netstandard which aren't available in the other platform, you can use compiler directives to write compatible code for each. 如果2.0或Netstandard中的某些内容在其他平台上不可用,则可以使用编译器指令为每个平台编写兼容的代码。

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

相关问题 使用.net标准2.0将.net4.0旧库转换为多个目标会产生HtmlString引用错误 - Converting .net4.0 old library to multiple targets with .net standard 2.0 gives HtmlString reference error 如何从 .NET Standard 2.0 和 UWP 获取进程内存? - How to get process memory from both .NET Standard 2.0 and UWP? 如何在.NET Standard 2.0库中包含资产文件? - How to include asset files in a .NET Standard 2.0 library? Roslyn 编译 - 如何引用 .NET 标准 2.0 class 库 - Roslyn compilation - how to reference a .NET Standard 2.0 class library .net 标准 2.0 库中的 EF Core DbContext - EF Core DbContext in .net standard 2.0 library .NET Standard 2.0 Class 库能否依赖于 .NET Framework 4.7.2? - Can .NET Standard 2.0 Class Library depend on .NET Framework 4.7.2? 汇编版本不匹配? (.Net Core 2.0与.NET Standard 2类库) - Mismatch in assembly versioning? (.Net Core 2.0 with .NET Standard 2 class library) 如何从log4net为.NET Standard 2.0类库设置FileAppender记录器? - How do I setup a FileAppender logger from log4net for a .NET Standard 2.0 class library? .NET Standard 2.0中的WriteableBitmap - WriteableBitmap in .NET Standard 2.0 如何访问 .NET Standard 2.0 DLL 中的文件? - How to access a file in .NET Standard 2.0 DLL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM