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

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

问题描述

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

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 和大量文档,经过多次谷歌搜索后,我发现唯一的方法是在运行时执行操作系统检测,但这既低效又乏味.在 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 的方式,将操作系统名称放在文件名中,例如用于 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

来源

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

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