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

5
有没有办法在C# 6中指定AttributeTargets.Method 属性以应用于表达式主体成员?考虑以下只读属性:
public bool IsLast { [DebuggerStepThrough] get { return _next == null; } }

缩写的语法将是:


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属性--这里只是举例。


可能是跳过表达式主体属性调试器的重复问题。 - StayOnTarget
2个回答

2

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


1

回答自己的问题:以下语法可以工作,但不如问题中显示的(未能正常工作的)尝试紧凑。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接