问题标签 [nameof]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
2737 浏览

c# - 用 nameof() 声明常量作为值

设想

我有一个用于声明string程序周围使用的常量的类:

本质上,常量的实际值是什么并不重要,因为它在分配和使用时使用。这只是为了检查。

常量名称是不言自明的,但我想尝试避免多次使用相同的字符串值。


我想做什么

我知道这nameof()是在编译时评估的,因此完全可以将const string' 值分配给nameof()a 成员。

为了省去写出这些神奇的字符串,我考虑过使用nameof()常量本身。

像这样:


问题...

nameof()我想除了重构之外,使用 , 并没有真正的好处?

nameof()分配常量时使用有什么影响吗?

我应该坚持只使用硬编码的字符串吗?

0 投票
1 回答
168 浏览

.net - 使用 nameof 在不知道类型的情况下访问泛型类的成员

参加这两个课程:

我可以很容易地做到这一点:

但我不能这样做:

我可以这样做:

但我不想。

有没有办法做我想做的事而不必以我不想做的方式去做?

0 投票
3 回答
321 浏览

c# - 被作用域成员隐藏时可以使用 nameof 吗?

考虑以下遗留代码:

现在想象一下这段代码的一部分正在被重构,我们想Blah通过以下方式重构:

此解析器不会选择上下文关键字nameof,并将解析为nameof给出编译时错误(没有现有重载)的私有方法。为什么?如果方法是 . 我会理解这一点nameof(object o)

有什么办法可以让编译器解析为内置的nameof?我需要重构重命名方法的代码吗?

上面的例子是一个非常简化的场景,实际上该nameof方法在反射中被广泛使用,并且需要花费时间和精力来确保在任何地方都正确地进行了重命名。

0 投票
2 回答
5893 浏览

c# - 如何更改类/属性名称?

例如:

现在,如果我调用nameof(Car)它返回“汽车”

但是如何让 nameof 返回 Name 属性中的值而不是类或方法的名称。例如:nameof(Car) == "something"nameof(Car.color) == "something_else"

问题:

为此修复:

这个序列化:

将会:

我知道我可以使用JsonProperty更改小写序列化,但我认为我可以更改类或属性的名称......

0 投票
0 回答
1327 浏览

typescript - 如何获取传递给函数调用的属性名称?打字稿

例子:

我想在控制台中打印:“某物”我传递给测试函数调用的变量的名称。

更新

变量在一个文件中,函数在另一个文件中...

解释原因很复杂......但我想避免硬编码另一个函数的参数,例如在测试函数中。

文件1:

文件2:

文件3:

我想避免那个硬编码的名字。我想通过名称抛出该函数。

0 投票
2 回答
1095 浏览

typescript - 如何从文件中获取所有接口名称?(打字稿)

我想从特定文件中获取所有接口名称。

例如:

文件1.ts

文件2.ts

我应该会回来的:["first","second","third"];

更新

我想在打字稿中建立一个像EntityFramework这样的机制;

但是我需要知道如何将接口/类的名称与 api 路径自动匹配,以知道我是否有要调用的 api 接口或类...

0 投票
0 回答
130 浏览

c# - nameof() operator for property of generic class

I often use the C# 6.0 nameof(...) operator in tests to determine, if e.g. the INotifyPropertyChanged.PropertyChanged is called for the right property. Sometimes I have a generic class implementing the INotifyPropertyChanged interface:

To get the name of the property Name by now I use a arbitrary type to get the name: nameof(ViewModel<string>.Name). This solution is possible but might result in problems, if the generic type parameter gets constrainted.

Is there a possibility to get the name without a specific generic type? I suggested something like nameof(ViewModel<T>.Name) or nameof(ViewModel<>.Name), but that doesn't work.

0 投票
2 回答
2692 浏览

c# - nameof in attribute

Considering Myatt attribute and MyObj class, it is somehow strange that ObjName property is known in scope of Myatt attribute. Isn't it?

Update :

Sorry, I'm wondering why the first case is not possible and the second is possible.

I get it now. Thanks.

0 投票
1 回答
747 浏览

wpf - 从属性路径获取 XAML 中的属性字符串名称

有没有办法在 XAML 中获取属性名称?

我发现 XAML 中不支持nameof

像这样服务的东西:

0 投票
1 回答
126 浏览

c# - 有没有办法从被调用方法中的调用方法中获取参数的名称?

以下代码打印“参数”。

有没有办法获得“myString”?