Conditional compilation symbol for a .NET Core class library

后端 未结 3 1986
隐瞒了意图╮
隐瞒了意图╮ 2021-01-11 14:45

I have created a .NET Core R2 class library and have some common code that I use for several different platforms.

Some of the code is not valid in the .NET Core plat

相关标签:
3条回答
  • 2021-01-11 15:00

    Since xproj was discontinued, here is how it is done in the new Visual Studio 2017 .csproj files.

    <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3' Or '$(TargetFramework)' == 'netstandard1.6' ">
        <DefineConstants>NET_CORE</DefineConstants>
    </PropertyGroup>
    

    Then instead of:

    private TypeInfo GetTypeInfo(Type type)
    {
        #if NETSTANDARD1_3 || NETSTANDARD1_6
            // Core
        #else
            // Full framework
        #endif
    }
    

    You can do:

    private TypeInfo GetTypeInfo(Type type)
    {
        #if NET_CORE
            // Core
        #else
            // Fullframework
        #endif
    }
    

    See here for more details on multi-targeting: Developing Libraries with Cross Platform Tools, How to Multitarget

    0 讨论(0)
  • 2021-01-11 15:04

    Conditional variables should be defined in your project.json file for RC2, and I have a sample project here,

    Port #SNMP from .NET Core RC1 to RC2

    But there are also predefined ones from this article,

    Developing Libraries with Cross Platform Tools

    0 讨论(0)
  • 2021-01-11 15:07

    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

    0 讨论(0)
提交回复
热议问题