【问题标题】:.NET framework in #if statement for compilation is not identifying net461用于编译的#if 语句中的 .NET 框架未识别 net461
【发布时间】:2018-10-27 22:06:21
【问题描述】:

我有 2 个控制台应用程序。一个在 .Net Core 2 中,一个在 .Net 461 中

我正在尝试确定我正在运行什么框架

如果我能够识别 .net 核心但它无法确定它 对于我的 .net 461 应用程序

我是否需要为 .net 框架添加其他信息

这是我所拥有的:

#if NET461
   Console.WriteLine("hello from net NET461");  // expecting this to execute, but it does not
#endif
#if NETCOREAPP2_0
   Console.WriteLine("hello from net Core");
#endif
#if NETSTANDARD2_0
   Console.WriteLine("hello from net NETSTANDARD2_0");
#endif

更新

虽然我没有得到我想要的\预期的结果。我真的只需要知道它是否正在运行.net core。所以我只是做了以下

#if NETCOREAPP2_0
   Console.WriteLine("hello from .Net Core");
#else
   Console.WriteLine("hello from Not .Net Core");
#endif

感谢大家的回复

【问题讨论】:

标签: c# .net .net-core .net-standard


【解决方案1】:

我发现 csproj 的结构也很重要。这对我有用。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

</Project>

【讨论】:

  • 请解释一下这有多重要?这不是标准生成的吗?
  • 在 Visual Studio 2019 中,当我创建一个新项目“Console App (.Net Framework)”时,我没有得到这种类型的 csproj 格式。我得到包含这一行的东西 v4.6.1 在这种情况下,#if NET461 不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2021-04-29
相关资源
最近更新 更多