Dot net core build platform / os special code or class files

I want to create a .net base library that will have platform-specific code for making win32 and osx calls, and when compiling the library for each target os, I want to include the correct os code.

I looked at corefx and a large number of documents, and after a lot of google searches, the only way I found was to run an OS definition at runtime, but this is inefficient and tedious. In the case of corefx, they have a complex build system, which seems to automatically transfer files defined by the platform, but which depend on msbuild, etc. Did I miss something?

How to conditionally configure code for the OS both at the file level and at the code level, for example:

#if osx
// come code
#endif

Or, according to the golangs method, by placing the os name in the file name, such as MyClass.osx.cs for the osx code and MyClass.cs for the non-platform, and the build process will include the correct files.

I am using Visual Studio code and project.json.

(I think in Visual Studio I could define each target platform and include my own definitions, but I'm on osx)

Thanks in advance.

+4
source share
1 answer

You can define an assembly definition in your project. json

"buildOptions": {
                "define": [ "PORTABLE328" ]
            }

eg:

{
    "frameworks":{
        "netstandard1.6":{
           "dependencies":{
                "NETStandard.Library":"1.6.0",
            }
        },
        ".NETPortable,Version=v4.0,Profile=Profile328":{
            "buildOptions": {
                "define": [ "PORTABLE328" ]
            },
            "frameworkAssemblies":{
                "mscorlib":"",
                "System":"",
                "System.Core":"",
                "System.Net"
            }
        }
    }
}

Now you can conditionally compile this target:

#if !PORTABLE328
using System.Net.Http;
using System.Threading.Tasks;
// Potentially other namespaces which aren't compatible with Profile 328
#endif

Source

+1
source

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


All Articles