How to set .NET Core in #if statement for compilation

拜拜、爱过 提交于 2019-11-28 20:59:57

问题


I created a multi targeted framework project. I use something like this :

  #if NET40
    Console.WriteLine("hello from net 4");
  #endif

But I cant find wildcard for .NET Core. I tried :

   #if NETCOREAPP1.0
     Console.WriteLine("hello from net Core");
   #endif

but it is not valid statement. Thanks.


回答1:


You need underscore _ instead of point :

NETCOREAPP1_0 or the more recent NETCOREAPP1_1 and NETCOREAPP2_0

The article https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/libraries includes a list for the different preprocessor symbols.

.NET Framework 2.0: NET20

.NET Framework 3.5: NET35

.NET Framework 4.0: NET40

.NET Framework 4.5 --> NET45

.NET Framework 4.5.1 --> NET451

.NET Framework 4.5.2 --> NET452

.NET Framework 4.6 --> NET46

.NET Framework 4.6.1 --> NET461

.NET Framework 4.6.2 --> NET462

.NET Standard 1.0 --> NETSTANDARD1_0

.NET Standard 1.1 --> NETSTANDARD1_1

.NET Standard 1.2 --> NETSTANDARD1_2

.NET Standard 1.3 --> NETSTANDARD1_3

.NET Standard 1.4 --> NETSTANDARD1_4

.NET Standard 1.5 --> NETSTANDARD1_5

.NET Standard 1.6 --> NETSTANDARD1_6




回答2:


Extension of Devon's answer for VS2017 .csproj files:

Looking at the table here, you can easily define constants by using regular expressions. So you don't need to think about updating the conditions if target frameworks are added/changed.

<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">
  <DefineConstants>NETFRAMEWORK</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netstandard\d'))">
  <DefineConstants>NETSTANDARD</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netcoreapp\d'))">
  <DefineConstants>NETCORE</DefineConstants>
</PropertyGroup>

Usage:

#if NETFRAMEWORK
    FrameworkSpecific();
#endif

#if NETSTANDARD
    StandardSpecific();
#endif

#if NETCORE
    CoreSpecific();
#endif



回答3:


You can define any custom conditional compilation symbols in the following way (project.json):

"frameworks": {
    "net40": {
      "buildOptions": {
        "define": ["NET_40"]
      }
    },
    "netstandard1.5": {
      "buildOptions": {
        "define": [ "NET_STANDARD" ]
      }

    }
}

This approach seems to be more practical because you may use the same conditional symbol for several targets, without need to write something like

#if NET20 && NET 40 && NET45



回答4:


For the new Visual Studio 2017 csproj project system...

You can find the full list of available symbols here: https://docs.microsoft.com/en-us/dotnet/core/tutorials/libraries#how-to-multitarget

You can create composite constant(s) for in your .csproj file like so:

  <PropertyGroup Condition="'$(TargetFramework)' == 'net451' Or '$(TargetFramework)' == 'net461' ">
    <DefineConstants>FULLFRAMEWORK;FULL</DefineConstants>
  </PropertyGroup>

Then you can use it in a #if compiler directive like so:

#if FULLFRAMEWORK
        private bool DoSomethingFullFrameworkSpecific()
        {
            var connectionStringSetting = ConfigurationManager.ConnectionStrings[connectionStringName];
            return connectionStringSetting != null;
        }
#endif



回答5:


While the the answer above this one is correct, it should be noted that there is a bug in the .NET Core xproj project type. When you define a conditional compilation symbol through the project settings, it defines the element as "defines", but this is incorrect. It should create an element called "define". You can work around the issue by editing the project.json manually.

I have logged this bug with Microsoft in two places. Please take the time to register your annoyance with Microsoft so that they eventually get around to fixing it and not causing this grief for others.

This thread has a detailed explanation of the problem with steps to repro, and screenshots: https://github.com/dotnet/cli/issues/4022#issuecomment-238777946

This is the Microsoft Connect bug report: https://connect.microsoft.com/VisualStudio/feedbackdetail/view/2983351/conditional-compilation-symbols-broken-in-net-core-projects#tabs



来源:https://stackoverflow.com/questions/38476796/how-to-set-net-core-in-if-statement-for-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!