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

Is there an OR operator in C# conditional compilation?

我目前正在构建一个 .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

是的。它与标准 if:

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

更多信息:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if