c# - How to get the name of a method at runtime? -


i'm using event system takes string callback method name. i'd avoid hardcoding strings. there way can method names of given class in runtime?

something similar to:

typeof(myclass).name; 

but methods perfect.

edit:

i've been googling problem , results seem people looking name of current executing method. isn't i'm looking - instead i'd name of other methods within same class.

it sounds you're talking nameof operator, added in c# 6.0.

msdn documentation

excerpt:

used obtain simple (unqualified) string name of variable, type, or member. when reporting errors in code, hooking model-view-controller (mvc) links, firing property changed events, etc., want capture string name of method. using nameof helps keep code valid when renaming definitions. before had use string literals refer definitions, brittle when renaming code elements because tools not know check these string literals.

a nameof expression has form:

if (x == null) throw new argumentnullexception(nameof(x)); writeline(nameof(person.address.zipcode)); // prints "zipcode” 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -