点网核心构建平台/操作系统特定的代码或类文件 [英] dot net core build platform/os specific code or class files

查看:90
本文介绍了点网核心构建平台/操作系统特定的代码或类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个.net核心库,该库将具有用于执行win32和osx调用的特定于平台的代码,并且在为每个目标os编译该库时,我想包括正确的os代码。

I want to build a .net core library that will have platform specific code for doing win32 and osx calls and when compiling the library for each target os, I want to include the correct os code.

我查看了corefx和许多文档,经过大量的搜索之后,我发现的唯一方法是在运行时执行OS检测,但这既效率低下又乏味。在corefx的情况下,他们有一个复杂的构建系统,该系统似乎可以自动提取特定于平台的文件,但依赖于msbuild等。我是否缺少某些东西?

I've looked at the corefx and lots of documents and after much googling the only way I've found is to perform OS detection at runtime but that is both inefficient and tedious. In the case of corefx, they have a complex build system that seems to automagically pull in platform specific files but that relies on msbuild and more. Am I missing something?

我如何有条件地将操作系统代码定位在文件级别或代码级别,例如:

How do I conditionally target code for a OS either at the file level or code level such as:

#if osx
// come code
#endif

或者按照golangs的方式,将os名称放在文件名中,例如osx代码使用MyClass.osx.cs,非平台使用MyClass.cs,构建过程将包括正确的文件本身。

Or, as per golangs way, by putting the os name in the filename such as MyClass.osx.cs for osx code and MyClass.cs for non platform stuff and the build process will take of including the correct files itself.

我正在使用Visual Studio代码和project.json。

I'm using Visual Studio code and project.json.

(我想在Visual Studio中我可以定义每个平台目标并包含我自己的定义,但是我在osx上。)

(I guess in Visual studio I could define each platform target and include my own defines but I'm on osx)

预先感谢。

推荐答案

您可以在project.json中定义构建定义。

You could define a build definition in your project.json

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

例如:

{
    "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 against that target:

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

来源

这篇关于点网核心构建平台/操作系统特定的代码或类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆