Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C# nameof operator reference instance property without instance?

Tags:

c#

c#-6.0

nameof

I regularly want to get the name of an instance property of a type, when I have no instance. Currently to do this, I use the following inhouse function which interprets the Expression[Func[T, object]] parameter and returns the property name:

var str = LinqExtensions.NameOf<ClientService>(x => x.EndDate); // Now str == "EndDate" 

However it seems a shame not to use the built in nameof operator.

Unfortunately it seems that the nameof operator requires either an instance, or, to reference a static properties.

Is there a neat way to use the nameof operator instead of our in house function? For example:

nameof(ClientService.EndDate) // ClientService.EndDate not normally syntactically valid as EndDate is instance member 

EDIT

I was completely wrong, the syntax nameof(ClientService.EndDate) as described actually works as is.

like image 832
Brendan Hill Avatar asked Jul 08 '16 02:07

Brendan Hill


People also ask

Does Can-C reverse cataracts?

A popular eye drop 'Can-C' containing N-alpha-acetylcarnosine (NAC) claims to reduce, reverse and slow the development of senile cataract. It was developed and is patented by Professor Babizhayev, a bio-physicist and Executive Director of Innovative Vision Products (IVP) [1].

Is Can-C good for dogs?

SAFE FOR HUMANS AND DOGS - Can-C is the first and only patented NAC eye drop that uses the exact formula proven effective in both animal and human trials, offering a non-invasive alternative to cataract surgery. EVERY BLINK HYDRATES and lubricates the eye and cornea.

Can-C Eye Drops benefits?

Can-C's ingredients help to soothe and rejuvenate tired eyes, help with macular degeneration, cataracts, and other age-related eye disorders. Can-C eye drops for cataracts contain lubricants, an antioxidant and an anti-glycating agent, N-Acetyl-Carnosine (NAC).

Can-C NAC?

The main ingredient of Can-C™ NAC eye drops is N-acetylcarnosine (NAC), a special analogue of di-peptide carnosine, a naturally occurring anti-oxidant nutrient. NAC is extremely effective at 'mopping up' free radicals, which accelerate aging in the body, including the growth of cataracts.


2 Answers

In the past, the documentation explicitly explained this, reading in part:

In the examples you see that you can use a type name and access an instance method name. You do not need to have an instance of the type[emphasis mine]

This has been omitted in the current documentation. However, the examples still make this clear. Code samples such as Console.WriteLine(nameof(List<int>.Count)); // output: Count and Console.WriteLine(nameof(List<int>.Add)); // output: Add show how to use nameof to obtain the string value with the name of an instance member of a class.

I.e. you should be able to write nameof(ClientService.EndDate) and have it work, contrary to your observation in the question that this would be "not normally syntactically valid".

If you are having trouble with the syntax, please provide a good Minimal, Complete, and Verifiable code example that reliably reproduces whatever error you're getting, and provide the exact text of the error message.

like image 179
Peter Duniho Avatar answered Oct 14 '22 04:10

Peter Duniho


Great answer by @Peter Duniho.

In case of name clashes, you can also do the following:

ClientService clientservice; var str = nameof(clientservice.EndDate); 

Not efficient, but curious enough.

like image 36
wonea Avatar answered Oct 14 '22 02:10

wonea