In C#, `nameof` expressions are a form of reflection. They return the string representation of the argument, as seen by the compiler.
Questions tagged [nameof]
100 questions
3
votes
3 answers
Why member access modifier does matter for nameof()?
I'm a little bit confused with nameof() operator. So for example I can't use class's private fields in nameof() in another class, but I can use public non static fields using non static property, so I don't need instantiated object.
Is it…

Rekshino
- 6,954
- 2
- 19
- 44
3
votes
2 answers
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?
[AttributeUsage(AttributeTargets.Property)]
public class MyAtt : Attribute
{
public MyAtt(string name)
…

Saeed
- 185
- 2
- 13
3
votes
0 answers
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:
public…

scher
- 1,813
- 2
- 18
- 39
3
votes
0 answers
EF dotted navigation properties using nameof
Having this model (All properties are Single (not Collection) navigation properties)
- University
- Department
- Classroom
- Teacher
In order to get the dotted name for each level we have to write it manually.
…

Mohsen Afshin
- 13,273
- 10
- 65
- 90
3
votes
1 answer
Find the literal resource key with the ResourceManager and nameof operator
I have to access my resource files weakly typed, that means I have to load/access the resources with the ResourceManager passing the full namespace + filename.
var rm = new ResourceManager("namespace.name.locale.brand",…

HelloWorld
- 4,671
- 12
- 46
- 78
3
votes
2 answers
Using nameof to get a setter method name in c#
Is it possible to get a setter method name using the new nameof operator?
public object Foo { get; set; }
public void Test()
{
var myMethod = GetType().GetMethod("set_Foo");
}
I guess GetType().GetMethod("set_" + nameof(Foo))…

FIF
- 223
- 1
- 2
- 6
3
votes
4 answers
Object.GetType() also returning project name
Trying to override ToString() and use the GetType() to return the type of the object being used. It is returning the information but it includes the NameSpace. The question is how can I strip off the NameSpace and only display the object name. …

J Hatt
- 55
- 3
2
votes
1 answer
Roslyn: SyntaxTree compilation fails, whereas string from SyntaxTree compilation works
[Edit] Solution:
Problem were solved by below answers. I used this part of code:
SyntaxFactory.IdentifierName(
SyntaxFactory.Identifier(
SyntaxFactory.TriviaList(),
SyntaxKind.NameOfKeyword,
"nameof",
"nameof",
…

Erjot
- 21
- 3
2
votes
1 answer
Returning name of the concrete implementation of a func<> delegate
tl;dr version: nameof(ToCelsius) returns ToCelsius. I want something that returns the name of the underlying function, ToCelsiusByA, though.
My ToCelsius is assigned a concrete implementation thus:
Func ToCelsius = ToCelsiusByA;
I…

PakiPat
- 1,020
- 14
- 27
2
votes
2 answers
How to get all interfaces names from a file? (Typescript)
I want to get all interfaces names from a specific file.
for example:
file1.ts
private interface first{
...
}
private interface second{
...
}
private interface third{
...
}
file2.ts
const interfacesList = GetInterfacesFrom(filePath); //in this…

Alex
- 1,013
- 1
- 13
- 27
2
votes
0 answers
How to get name of property passed to function call? Typescript
example:
var something = "some text";
test(something);
function test(param){
printParamName(param);
}
//console
"something"
I want to print in console: "something"
the name of variable which I pass to that call to test function.
UPDATE
The…

Alex
- 1,013
- 1
- 13
- 27
2
votes
2 answers
Should I provide a method with a member name with nameof, or should I depend on CallerMemberName to do it for me?
How is CallerMemberName implemented?
I get what it does - it allows us to keep magic strings out of our code - but should it be used over nameof and what is more performant?
Whats the difference/how does CallerMemberName exactly work?

Mafii
- 7,227
- 1
- 35
- 55
1
vote
0 answers
C# How to get properties names as strings of generic class using nameof
C# How to get properties names of generic class using nameof without specifing a type.
I have class as follow:
public class Example where T : class
{
public DateTime CreatedAt { get; set; } = DateTime.Now;
}
And I want to get "CreateAt" as…

ElConrado
- 1,477
- 4
- 20
- 46
1
vote
2 answers
How to get the full name of a namespace as a string in C#?
I'm trying to get the full name of a namespace and convert it to a string.
Specifically, I have the namespace DevExpress.Xpf and I would like nameof2(DevExpress.Xpf) or some equivalent to return "DevExpress.Xpf", rather than the "Xpf" that nameof…

EagleBirdman
- 140
- 1
- 10
1
vote
0 answers
Downsides of initializing readonly string with its own name per nameof()?
To clarify, is there any downside to initializing a string like this:
public static readonly string myString = nameof(myString);
I've recently come across this method and thought it was quite clever, since you can rename the variable and…

M-Expunged
- 71
- 1
- 6