c# - .NET Core 类库的条件编译符号

标签 c# conditional-compilation .net-core-rc2

我创建了一个 .NET Core R2 类库,并拥有一些用于多个不同平台的通用代码。

某些代码在 .NET Core 平台中无效,因此我希望将其包裹在一个条件编译符号中。我首先在 Internet 上搜索以查看是否可以找到内置符号(例如 Silverlight 应用程序的 SILVERLIGHT 和 Windows 8 应用程序的 WINFX_CORE),但找不到任何信息,所以我决定创建自己的符号.这似乎也不起作用。

从我阅读的所有内容来看,添加和使用符号应该很容易。只需在项目属性 → Build 选项卡中为条件编译符号添加一个值。我这样做了,但它似乎不起作用。这里有几个屏幕截图...

Enter image description here

Enter image description here

请注意,我在条件编译符号中添加了一个 NET_CORE 值,但是当我在代码中使用它时,代码不会被忽略。

  1. 是否有 .NET Core 平台的内置符号(我使用的是 R2)?

  2. 如果没有,我在创建自己的符号时做错了什么?

.xproj 文件:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
  <PropertyGroup Label="Globals">
    <ProjectGuid>253184d7-9b42-4233-a871-8cfa3ee9e83e</ProjectGuid>
    <RootNamespace>Linq2Db.NetCore</RootNamespace>
    <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
    <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <SchemaVersion>2.0</SchemaVersion>
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

更新:我能够使用提供的答案中的链接解决此问题。这是详细信息...

最初 project.json 文件看起来像这样......

{
  "dependencies": {
        "NETStandard.Library": "1.5.0-rc2-24027"
  },
  "frameworks": {
    "netstandard1.5": {
      "imports": "dnxcore50"
    }
  },
  "buildOptions": {
    "defines": [ "NET_CORE" ]
  }
}

我通过将其更改为...解决了这个问题

{
  "frameworks": {
    "netstandard1.5": {
      "imports": "dnxcore50",
      "dependencies": {
        "NETStandard.Library": "1.5.0-rc2-24027"
      },
      "buildOptions": {
        "define": [ "NET_CORE" ]
      }
    }
  }
}

最佳答案

由于 xproj 已停产,因此在新的 Visual Studio 2017 .csproj 文件中它是如何完成的。

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

然后代替:

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

你可以这样做:

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

有关多目标的更多详细信息,请参见此处: Developing Libraries with Cross Platform Tools, How to Multitarget

关于c# - .NET Core 类库的条件编译符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38040466/

相关文章:

条件编译: compile once if macro is present at least once

unit-testing - AppHarbor 可以在构建过程中设置条件编译符号吗?

ASP.Net 5 RC2 配置节绑定(bind)

c# - ASP.NET Core HTTPRequestMessage 返回奇怪的 JSON 消息

c# - ASP.NET Core RC2 区域未发布

c# - 运行线程时我的 Try Catch block 应该在哪里?

c# - 在 ASP .NET Visual Studio 中使用 SAS 存储过程。 SAS 集成技术?

c++ - Makefile,根据OS修改命令

c# - 有没有办法在 Visual Studio 搜索/替换中插入换行符?

c# - AspNet 成员(member)资格 - 集中登录