简体   繁体   English

我可以根据.NET Core中的运行时标识符定义常量吗?

[英]Can I define constants based on the runtime identifier in .NET Core?

I have a .NET Core Console application. 我有一个.NET Core Console应用程序。 My goal here is to be able to conditionally DLLImport a function and call it, but only on Windows runtimes. 我的目标是能够有条件地DLLImport一个函数并调用它,但只能在Windows运行时。

I thought maybe if I could access the runtime identifier in the csproj file, I could conditionally define a constant for that runtime, then in my c# I could surround the DLLImport and calls in #if / #endif blocks. 我想如果我可以访问csproj文件中的运行时标识符,我可以有条件地为该运行时定义一个常量,然后在我的c#中我可以包围DLLImport并调用#if / #endif块。

Is it possible to set compilation constants within a csproj based on the runtime the project is being built for? 是否可以根据构建项目的运行时在csproj设置编译常量? This is specifically for an SDK-style Project format (that starts with <Project Sdk="Microsoft.NET.Sdk"> ) that is targeting .NET Core. 这是针对以.NET风格为目标的SDK风格的项目格式(以<Project Sdk="Microsoft.NET.Sdk"> )。

Note: this question gets close, but is for project.json style projects. 注意: 这个问题很接近,但是对于project.json样式项目。

Alternately, is there a better approach to accomplish my goal? 或者,是否有更好的方法来实现我的目标?

If you are building and publishing for different runtimes by passing different --runtime options (MSBuild property RuntimeIdentifier ), you can condition on that property in the csproj file (allowing you to use #if BUILT_FOR_WINDOWS in your C# code): 如果您通过传递不同的--runtime选项(MSBuild属性RuntimeIdentifier )来构建和发布不同的运行时,则可以在csproj文件中对该属性进行条件限制(允许您在C#代码中使用#if BUILT_FOR_WINDOWS ):

<PropertyGroup>
  <DefineConstants Condition="'$(RuntimeIdentifier)' == 'win-x64'">$(DefineConstants);BUILT_FOR_WINDOWS</DefineConstants>
</PropertyGroup>

However you can also test the current OS at run time using: 但是,您还可以使用以下命令在运行时测试当前操作系统:

using System.Runtime.InteropServices;
…

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    // call windows function here
}
else
{
    // do something else here
}

As long as a function marked with [DllImport(…)] is not called on an OS where the library / method cannot be found, there shouldn't be any problems. 只要在无法找到库/方法的OS上调用标有[DllImport(…)]的函数,就不会有任何问题。 Do note that DllImport() can also probe for different libraries depending on the os - so DllImport("foo") would check for foo.dll , foo.dylib , libfoo.so etc. 请注意, DllImport()也可以根据操作系统探测不同的库 - 所以DllImport("foo")会检查foo.dllfoo.dyliblibfoo.so等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# /.Net.Core 中定义基于平台(Linux vs Windows)的编译时间常数? - How to define compile time constants based on platform (Linux vs Windows) in C# / .Net.Core? 我可以在 .NET Core 可执行文件中嵌入文件并在运行时读取它吗? - Can I embed a file in a .NET Core executable and read it at runtime? 如何检测 .NET Core 中的运行时代码生成? - How can I detect runtime code generation in .NET Core? 如何在不输入数字的情况下定义数学常数? - How can I define mathematical constants without typing out the number? 如何将 dbUrl、其他常量设置为 .net core 2.2 项目的环境变量,以及如何在我的 .net core 2.2 应用程序中读取这些常量? - How can i set dbUrl, other constants as einvironment variable for .net core 2.2 project and how to read those contants inside my .net core 2.2 app? 如何将 C# 类作为字符串 UserInput,并使其可用于 ASP.NET Core 运行时? - How can I take a C# Class, as a string UserInput, and make it available to the ASP.NET Core runtime? 如何在运行时在ASP.Net Core中创建URL重写? - How can I create url rewrites in ASP.Net Core at runtime? 如何在 ASP.NET Core 5 运行时动态重新配置服务? - How can I dynamically re-configure a Service during runtime in ASP.NET Core 5? 在WPF XAML中,如何连接2个常量,以便我可以使用预定义路径? - In WPF XAML how can I concatenate 2 constants so I can use pre-define paths? NET Core安装运行时 - NET Core install runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM