Questions tagged [nameof]

In C#, `nameof` expressions are a form of reflection. They return the string representation of the argument, as seen by the compiler.

What is the purpose of nameof?

100 questions
1
vote
2 answers

How to create something similar to nameof() c#

What I have: public class ClassA: Class C { public string Prop1 {get;set;} public ClassB Prop2 {get; set;} } public class ClassB: Class C { public int Prop3 {get; set;} } I want a method like nameof() which will behave something like…
SunainaDG
  • 109
  • 1
  • 14
1
vote
2 answers

Is it possible to write a wrapper for the C# nameof operator?

I read a related question and answer about the nameof operator, but it didn't help me, so I asked it here. I want to write a wrapper for the C# nameof operator so not only will it will return the name of a class property, but also concatenate it…
yekanchi
  • 813
  • 1
  • 12
  • 30
1
vote
3 answers

How to get nameof() of the passed parameter inside of the method?

I am searching for solution for my project, where I have a Dictionary and a helper method witch is trying to get some specific value from this dictionary. As you can see below, if I don't have a stringKey, I am trying to get that…
1
vote
2 answers

IronRuby fails to compile c# project which uses 'nameof' operator

I have a large WPF application with many solutions and each solution has many projects. IronRuby (v1.0.4) script is used to compile all the projects in order. Problem Statement: IronRuby script fails to compile projects which use 'nameof' operator…
RDV
  • 957
  • 13
  • 28
1
vote
1 answer

VB.Net Equivalent of C# nameof usage

Is there a VB.Net equivalent of following C# nameof usage: [MyAttribute(nameof(MyProperty))] public class MyClass { private int MyProperty { get; } } Note: MyClass is a generic class and MyProperty is private. I can only work out the…
Weifen Luo
  • 603
  • 5
  • 13
1
vote
0 answers

C# getting nameof() value back as a variable

I wanted to use nameof() to get variable name, to later use it in something like an Undo/Redo method. In my specific case I have 2 different classes, which I want to use the Undo/Redo function. My first thought was to push changes to a stack in a …
Samuel Novelinka
  • 328
  • 3
  • 10
1
vote
1 answer

Get class's property name at compile time without object instantiation

Is it possible to get the name of class's property (attention!) at compile time and without object instantiation? With instantiation it can easely be done with nameof(): class DummyClass { public int DummyProperty { get; set; } } void…
Rekshino
  • 6,954
  • 2
  • 19
  • 44
1
vote
1 answer

Why is this expression working? (C# 6.0)

nameof(ServiceResult.Result), where ServiceResult is my custom type and Result is the field of this type. ServiceResult is just a declaration of type, it doesn't have operator new and (), but official page of MS says nameof…
Adventurer
  • 73
  • 1
  • 4
1
vote
3 answers

nameof with casting

I want to use nameof in order to get the name of a property. The following work: DerivedClass EC = baseClassObj as DerivedClass; nameof(EC.propertyX) but this doesn't work: nameof((baseClassObj as DerivedClass).propertyX) with the compile error…
Yuval Levy
  • 2,397
  • 10
  • 44
  • 73
1
vote
1 answer

How to get the name of string parameter in C# Win Forms application using a method?

For some reasons i want to get the name of some variables in my C# Win Forms application. I used the following code private void button1_Click(object sender, EventArgs e) { string my_name = "my_value"; …
EgyEast
  • 1,542
  • 6
  • 28
  • 44
1
vote
1 answer

Get property string name in XAML from property path

Is there way to get name of property in XAML? I found there is no support for nameof in XAML. Something serving like this:
Yarl
  • 728
  • 1
  • 7
  • 26
1
vote
2 answers

How to change class/property name?

For example: public class Car{ public string color {get; set;} public int VINCode {get;set;} } Now if I call nameof(Car) it returns "Car" [Name("something")] public class Car{ [Name("something_else")] public string color…
Alex
  • 1,013
  • 1
  • 13
  • 27
1
vote
5 answers

Get the names of parameters method

I have a method with a ton of parameters. Some of them are optional. So, in order to use this method easily I use the optional parameter feature. Moreover, this method builds a Dictionary with name of parameter as key of the…
Florian
  • 4,507
  • 10
  • 53
  • 73
1
vote
3 answers

Using NameOf with a variable

Why doesn't any of the following compile: NameOf(value.GetType()) NameOf(GetType(value)) This expression doesn't have a name. How can I get the string name of the type of the value that was passed? ToString() might be overloaded, so can't use…
dotNET
  • 33,414
  • 24
  • 162
  • 251
0
votes
1 answer

How to access name of named tuple in Typescript

I want to access the name from a named tuple to use within one of my functions. Is there a way to access the name only, similar to how you can access the type only by using the index of the tuple element. type NamedThing = [emailId: string,…
sayandcode
  • 1,775
  • 1
  • 11
  • 24