C#条件编译中是否有OR运算符?

问题描述 投票:0回答:1

我目前正在构建一个.NET程序集,该程序应在.NET 4.5和至少两个.NET Core版本(.NET Core 2.1和.NET Core 3.0)中工作。

我正在像这样使用条件编译:

#if NET45
        //Use as System.Web.HttpContext
        isHttps = context.Request.IsSecureConnection;
        IPAddress fromIp = IPAddress.Parse(context.Request.UserHostAddress);
        string path = context.Request.Path;
#elif NETCOREAPP2_1
        //Use as Microsoft.AspNetCore.Http.HttpContext
        isHttps = context.Request.IsHttps;
        IPAddress fromIp = context.Request.HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
        string path = context.Request.Path;
#elif NETCOREAPP3_0
        //Use as Microsoft.AspNetCore.Http.HttpContext
        isHttps = context.Request.IsHttps;
        IPAddress fromIp = context.Request.HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
        string path = context.Request.Path;
#endif

由于NETCOREAPP2_1和NETCOREAPP3_0的代码是相同的,所以我可以使用类似以下的代码:

#if NET45
        //...
#elif NETCOREAPP2_1 [OR] NETCOREAPP3_0
        //...
#endif    

但是,此语法不起作用。

在这样的条件编译中是否存在有效的语法来包含OR运算符?

注:由于这涉及ASP.NET请求管道,因此我认为.NET Standard不是一个选择。您可能希望及时查看代码:https://github.com/suterma/SqlSyringe/blob/f7df15e2c40a591b8cea24389a1ba8282eb02f6c/SqlSyringe/Syringe.cs

c# asp.net compilation .net-standard conditional-compilation
1个回答
2
投票

是的。与标准if中的相同:

#if NET45
    // ...
#elif (NETCOREAPP2_1 || NETCOREAPP3_0)
    // ...
#endif

更多在这里:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

© www.soinside.com 2019 - 2024. All rights reserved.