方法C#表达式身份成员的属性

时间:2017-06-10 21:14:59

标签: c# syntax properties attributes c#-6.0

有没有办法在C#6的attribute上指定AttributeTargets.Method expression-bodied member?请考虑以下只读属性:

public bool IsLast { [DebuggerStepThrough] get { return _next == null; } }

缩写的synax将是:

public bool IsLast => _next == null;

但是似乎没有地方放置方法属性。以下工作均不起作用:

[DebuggerStepThrough]
public bool IsLast => _next == null;      // decorates property, not method

public bool IsLast => [DebuggerStepThrough] _next == null;    // error

public bool IsLast [DebuggerStepThrough] => _next == null;    // error



[编辑:]建议这不是'Skip expression bodied property in debugger'的重复,因为此问题询问任何方法 - 通常是合适的属性,而不仅仅是DebuggerStepThrough属性 - 特别是在这里作为示例给出 -

2 个答案:

答案 0 :(得分:2)

您可以将AttributeTargets.Method属性应用于表达式身体方法,但不能应用于表达式身体属性

答案 1 :(得分:0)

回答我自己的问题:以下语法有效,但它不像问题中显示的(非工作)尝试那么紧凑。

public bool IsLast { [DebuggerStepThrough] get => _next == null;  }
相关问题