.net - C#, use reflection to get each case name in switch -



.net - C#, use reflection to get each case name in switch -

here's contrived illustration of string literal switch statement:

static string getstuff(string key) { switch (key) { case "thing1": homecoming "oh no"; case "thing2": homecoming "oh yes"; case "cat": homecoming "in hat"; case "wocket": homecoming "in pocket"; case "redfish": homecoming "blue fish"; case "onefish": homecoming "two fish"; default: throw new notimplementedexception("the key '" + key + "' not exist, go inquire dad"); } }

you idea.

what i'd love print each of literal strings each of cases via reflection.

i've not done plenty reflection know how intuitively. i'm not sure if reflection can kind of thing @ all.

can done? if so, how?

no, can't read il (which looking for) reflection apis.

the closest can come methodinfo.getmethodbody (methodbody class) give byte array il. implementation details of method need library reads il cecil.

the switch string implemented using if or dictionary based on number of choices - see are .net switch statements hashed or indexed?. if reading il take account.*

note should utilize other mechanisms represent info rather trying read compiled code. i.e. utilize dictionary represent choices suggested mikeh's answer.

* info on switch implementation found mad sorcerer.

c# .net reflection switch-statement

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -