C# 可选枚举类型属性 setter 在经典 .net 中编译但不在 .net 核心中编译? (cs0019)

标签 c# .net enums visual-studio-2015 asp.net-core

我正在尝试将带有此属性 setter 的代码移植到 .net 核心。在这种情况下,我得到了一个我没想到的错误。为什么?有什么不同?

    /// <remarks>This uses the native .NET datatype, rather than the FHIR equivalent</remarks>
    [NotMapped]
    [IgnoreDataMemberAttribute]
    public Hl7.Fhir.Model.RemittanceOutcome? Outcome
    {
        get { return OutcomeElement != null ? OutcomeElement.Value : null; }
        set
        {
            if(value == null) // CS00019
              OutcomeElement = null; 
            else
              OutcomeElement = new Code<Hl7.Fhir.Model.RemittanceOutcome>(value);
            OnPropertyChanged("Outcome");
        }
    }

错误在 Enum? 可选类型的 setter 中:

    CS0019  Operator '==' cannot be applied to operands of type 
    'RemittanceOutcome?' and '<null>'   

RemittanceOutcome 类型是public enum RemittanceOutcome。对于可选的枚举,它变得不可能还是现在的实现有所不同?我是否遇到了 ASP.net core 1.0.0 rtm 中的编译器错误?

这是在带有 project.json 的 .net 核心库中,如下所示:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Newtonsoft.Json": "9.0.1",
    "System.ComponentModel.Annotations": "4.1.0",
    "System.ComponentModel.Primitives": "4.1.0",
    "System.Diagnostics.Contracts": "4.0.1",
    "System.Net.Requests": "4.0.11"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  },

  "buildOptions": {
    "define": [],
    "nowarn": [ "CS3016" ]
  }
}

将此代码移植到 dnxcore50(aspnet core 1.0.0 rtm)还有其他一些惊喜,但这个似乎足以让初学者咀嚼。

示例项目:https://github.com/wpostma/fhir-net-api - 分支 dnxcore50。

最佳答案

我会说你遇到了一个错误。这应该是可行的。要确认你可以尝试

if(value.HasValue)

相反,看看它是否有效?

关于C# 可选枚举类型属性 setter 在经典 .net 中编译但不在 .net 核心中编译? (cs0019),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38191166/

相关文章:

c# - 如何响应.write bytearray?

c# - 将用户限制在 OpenFileDialog 中的特定目录

.net - 使用 OnKeyUp 在 TextBox 中按下 Enter 时避免 Windows 'Ding'

c# - 无法从 TPanel 获取 TEdit 句柄

c# - 使用 Parallel.ForEach 和 Task.Factory.StartNew 之间的主要区别是什么

在界面中使用枚举的 typescript

enums - 如何使 CRMSvcUtil.exe 生成不重复、无错误的早期绑定(bind)选项集?

java - 枚举的常量值添加匿名类

c# - 在 Click-Once 应用程序中包含 SQLite?

c# - 将数据表值保存为空字符串而不是 null