[英]Why is this expression working? (C# 6.0)
nameof(ServiceResult<object>.Result)
, where ServiceResult<object>
is my custom type and Result
is the field of this type. nameof(ServiceResult<object>.Result)
,其中ServiceResult<object>
是我的自定义类型,而Result
是此类型的字段。 ServiceResult<object>
is just a declaration of type, it doesn't have operator new and (), but official page of MS says nameof
accepts variable and its members. ServiceResult<object>
只是类型的声明,它没有运算符new和(),但是MS的官方页面上说nameof
接受变量及其成员。 Why this expression works? 为什么此表达式有效? I didn't see such declarations before.
我以前没有看到这样的声明。
The spec you mentioned is probably an old one, C# 6.0 nameof
operator reference: 您提到的规范可能是旧的C#6.0
nameof
运算符参考:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof
The argument to
nameof
must be a simple name, qualified name, member access, base access with a specified member, or this access with a specified member.nameof
的参数必须是简单名称,限定名称,成员访问权限,具有指定成员的基本访问权限或具有指定成员的此访问权限。 The argument expression identifies a code definition, but it is never evaluated.参数表达式标识代码定义,但从不对其求值。
In your case, it's an expression. 就您而言,这是一种表达。 Similar to
如同
nameof(C.Method2) -> "Method2"
from the examples list in that article. 从该文章的示例列表中。
Examples 例子
using Stuff = Some.Cool.Functionality
class C {
static int Method1 (string x, int y) {}
static int Method1 (string x, string y) {}
int Method2 (int z) {}
string f<T>() => nameof(T);
}
var c = new C()
nameof(C) -> "C"
nameof(C.Method1) -> "Method1"
nameof(C.Method2) -> "Method2"
nameof(c.Method1) -> "Method1"
nameof(c.Method2) -> "Method2"
nameof(z) -> "z" // inside of Method2 ok, inside Method1 is a compiler error
nameof(Stuff) = "Stuff"
nameof(T) -> "T" // works inside of method but not in attributes on the method
nameof(f) -> "f"
nameof(f<T>) -> syntax error
nameof(f<>) -> syntax error
nameof(Method2()) -> error "This expression does not have a name"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.