[英]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.Reflection
和Microsoft.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.