Get reference to some class field via reflection in C# -


class mapper {      list<int> l;      static dictionary<string, string> legend = new dictionary<string, string>(){{"list1", "my_fancyugly_n4medlst777"}}      public mapper(mainwindow w)      {          l = w.(legend["list1"]);    //how refference?      } } 

in other words, want refference field (or method, property etc) name don't know until runtime. know can value of field, that's not want. possible , how that?

you can use fieldinfo fi = typeof(mainwindow).getfield("fieldname"); fieldinfo object. call fi.getvalue(w); value. same principle applies sorts of members , operations on them.

this slow @ runtime , makes harder catch errors @ compile time, make sure have reason use reflection here!


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -