dotNET dotNET - 1 year ago 182 Question

Using NameOf with a variable

Why doesn't any of the following compile:


This expression doesn't have a name.

How can I get the string name of the type of the value that was passed?
might be overloaded, so can't use that.

Answer Source

You can't use NameOf in that case because this new operator gets the name of a member from the member itself.

GetType doesn't return the member but the type metadata of a class, structure, interface or enumeration.

Thus, you'll be able to get the whole type name getting the Type.Name property value: value.GetType().Name