How to create a library designed for .NET 2.0 and .NET Standard?

I have a small library that currently supports .NET 2.0 +.

I do not use any functions of the later versions of the framework, so it would be nice to support support 2.0, but I also want to configure .NET Core (or, more precisely, .NET Standard).

I tried to add both frames to project.json:

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

But NuGet packages that my library should run on .NET Standard ( System.Reflectionand Microsoft.AspNetCore.WebUtilities) are not compatible with .NET 2.0.

How can I solve this problem without supporting two completely separate projects with almost identical code?

+4
2

, Microsoft.AspNetCore.* .NET Standard.NET 4.5.

.NET 4.5 - , System.Runtime, .NET Core. , . ASP.NET Core .

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"
  }
}

netstandard1.3 Microsoft.AspNetCore.WebUtilities, .

NameOf.AspNetLegacyPackage - , , Microsoft.AspNetCore.WebUtilities, , .NET Framework 2.0, . , .

#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

, .NET Framework 2.0 4.5.1, Microsoft.AspNetCore.WebUtillities (. NuGet )

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

, Shared, , .NET 2.0 , NetStandard. 2.0 Netstandard , , .

+1

Source: https://habr.com/ru/post/1665405/


All Articles