6.0版获得了nameof的新功能,但我不能理解它的目的,因为它只是接受变量名并在编译时将其更改为字符串。

我认为它在使用<T>时可能有一些目的,但当我尝试命名(T)时,它只是打印我一个T而不是使用的类型。

知道目的吗?


当前回答

操作符名称的目的是提供工件的源名称。

通常源名称与元数据名称相同:

public void M(string p)
{
    if (p == null)
    {
        throw new ArgumentNullException(nameof(p));
    }
    ...
}

public int P
{
    get
    {
        return p;
    }
    set
    {
        p = value;
        NotifyPropertyChanged(nameof(P));
    }
}

但情况并非总是如此:

using i = System.Int32;
...
Console.WriteLine(nameof(i)); // prints "i"

Or:

public static string Extension<T>(this T t)
{
    return nameof(T); returns "T"
}

我给它的一个用途是命名资源:

[Display(
    ResourceType = typeof(Resources),
    Name = nameof(Resources.Title_Name),
    ShortName = nameof(Resources.Title_ShortName),
    Description = nameof(Resources.Title_Description),
    Prompt = nameof(Resources.Title_Prompt))]

事实上,在这种情况下,我甚至不需要生成的属性来访问资源,但是现在我有一个编译时检查资源是否存在。

其他回答

ASP。NET核心MVC项目使用AccountController.cs和ManageController.cs中的name和RedirectToAction方法来引用控制器中的动作。

例子:

return RedirectToAction(nameof(HomeController.Index), "Home");

这句话的意思是:

return RedirectToAction("Index", "Home");

和takes将用户带到'Home'控制器中的'Index'动作,即/Home/Index。

nameof的另一个用例是检查标签页,而不是检查索引,你可以检查标签页的Name属性,如下:

if(tabControl.SelectedTab.Name == nameof(tabSettings))
{
    // Do something
}

不那么乱:)

它对ArgumentException及其衍生物非常有用:

public string DoSomething(string input) 
{
    if(input == null) 
    {
        throw new ArgumentNullException(nameof(input));
    }
    ...

现在,如果有人重构输入参数的名称,异常也将保持最新。

在以前必须使用反射来获取属性或参数名称的某些地方,它也很有用。

在你的例子中,nameof(T)获取类型参数的名称-这也很有用:

throw new ArgumentException(nameof(T), $"Type {typeof(T)} does not support this method.");

nameof的另一种用法是用于枚举——通常如果你想要枚举的字符串名称,你可以使用.ToString():

enum MyEnum { ... FooBar = 7 ... }

Console.WriteLine(MyEnum.FooBar.ToString());

> "FooBar"

这实际上相对较慢,因为. net保存枚举值(即7)并在运行时查找名称。

用nameof代替:

Console.WriteLine(nameof(MyEnum.FooBar))

> "FooBar"

现在。net在编译时将枚举名称替换为字符串。


还有一种用法是INotifyPropertyChanged和日志记录——在这两种情况下,你都想把你调用的成员的名字传递给另一个方法:

// Property with notify of change
public int Foo
{
    get { return this.foo; }
    set
    {
        this.foo = value;
        PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Foo));
    }
}

还是……

// Write a log, audit or trace for the method called
void DoSomething(... params ...)
{
    Log(nameof(DoSomething), "Message....");
}

nameof关键字的用法之一是在wpf中以编程方式设置Binding。

要设置绑定,你必须设置路径字符串和nameof关键字,可以使用重构选项。

例如,如果你在你的UserControl中有IsEnable依赖属性,你想把它绑定到你的UserControl中某些复选框的IsEnable上,你可以使用这两个代码:

CheckBox chk = new CheckBox();
Binding bnd = new Binding ("IsEnable") { Source = this };
chk.SetBinding(IsEnabledProperty, bnd);

and

CheckBox chk = new CheckBox();
Binding bnd = new Binding (nameof (IsEnable)) { Source = this };
chk.SetBinding(IsEnabledProperty, bnd);

很明显,第一个代码不能重构,但第二个代码……

你的问题已经表达了目的。您必须看到,这对于记录日志或抛出异常可能很有用。

例如:

public void DoStuff(object input)
{
    if (input == null)
    {
        throw new ArgumentNullException(nameof(input));
    }
}

这很好。如果我更改了变量的名称,代码将中断,而不是返回带有错误消息的异常。


当然,用途并不局限于这种简单的情况。只要对变量或属性的名称进行编码是有用的,就可以使用nameof。

当您考虑各种绑定和反射情况时,其用途是多方面的。这是一种将运行时错误带到编译时的极好方法。