Search code examples
c#.net.net-coreportable-class-library.net-standard

Support .NET 4.5 and DotNetCore


How can I support .NET 4.5 and DotNetCore?

Is a PCL for shared code the best way forward (if at all possible to target DNX + .NET 4.5)?

Basically I have a library I publish as a NuGet package which still needs to support .NET 4.5.2, but I want to use this library in a DotNetCore application as well.

Is this something the .NET Standard will help with? Or is .NET standard only for .NET Core+?


Solution

  • In .NET Core you can select your build target, and library used. All those changes are made in project.json.

    To add support for .NET 4.52 you need to modify your project.json to look something like this

    "frameworks": {
    "netcoreapp1.0": { //Support for .NET Core
      "imports": "dnxcore50",
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    },
    "net452": { //Support for .NET 452
    }
    

    To add platform specific code for .NET 452 use

    #if NET452
    ...
    #endif
    

    To run with .NET 452 use this command

    dotnet run -f NET452
    

    On MSDN they have pretty good documentation about this topic and a lot of others.

    This Article also explains very good how to manage Core and normal .NET code in one project.