【问题标题】:Lambda properties in C# - "Invalid token in class, struct, or interface member declaration"C# 中的 Lambda 属性 - “类、结构或接口成员声明中的标记无效”
【发布时间】:2016-11-23 18:29:03
【问题描述】:

在我的项目中,属性是这样分配的:

[ConfigurationProperty("message", IsRequired = true)]
        public string message=> this["message"] as string;

当我编译我的代码时,我得到了这个错误:

类、结构或接口成员中的标记“消息”无效 声明错误 4 标识符应为

【问题讨论】:

  • 您是否针对正确版本的 C# (6.0) 进行编译?
  • 从您的标签中,可以安全地假设您使用 C#6 (MSBuild 14)?

标签: c# c#-4.0 c#-3.0


【解决方案1】:

表情丰富的成员were introduced in C# 6.0。您必须针对该版本进行编译或使用旧的成员语法。

作为一种解决方法,您可以这样做:

[ConfigurationProperty("message", IsRequired = true)]
public string message {
    get {
        return this["message"] as string;
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2014-04-07
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多