What is the best way to have .NET Framework & .NET Core specific code

We have a commercial library that we are revising to run on .NET Core also .

Which leads to the question, how do we handle the code that is .NET Framework only and .NET Core only?

First for distribution, we’ll use NuGet and it’ll have a different set of file(s) depending…

Second, how do I build this? We have 3 (so far) static methods that are platform specific, all in one of the DLLs (the library is 4 DLLs).

I’ve thought of the following:

  1. The code does a runtime check in each method and calls the appropriate code. This may not work because it will try to load references from the other platform when the class is first instantiated.
  2. Have a wrapper class and that class’ code will call to the appropriate class depending on the platform. This I think would avoid the loading non-existent packages issue.
  3. Use reflection to create & call the appropriate classes.
  4. Create two WindwardPlatform.dll libraries, each with identical public classes, version number, & strong naming.
  5. Something else???

thanks - dave

I believe the best option is to use multi-targeting and conditional compilation. This article has some guidance about the recommended ways to do that.

Use .NET Standard 2.0 if it supports everything you need as its the simplest approach.

If you need specific .NET Framework and .NET Core features, you can create two library projects, have a common code folder that each project references the same code files from. Then use compiler directives to handle differences. This is how you would do it in C/C++ and it applies well in C# if needed.

.NET Foundation Website | Blog | Projects | Code of Conduct