c# - Accessing variable from string name -
i accessing variable der
, can or set through local _der
.
the setvalues
gets string name on parameter wanted change(and list local list should set to)
the string comes "global" datastore name, change local (der->_der).
now, how in setvalues
, "convert" string name lparam
, actual local parameter want set?
hope u understand , can
[subscribe] public storablelist<double> der{ get{return _der;} set{_der = value;} } public static string converttolocalparameter(string input) { if (string.isnullorempty(input)) { throw new argumentexception("argh"); } return "_" + char.tolower(input[0]) + input.substring(1); } public void setvalues(string listname, storablelist<double> values) { string lparam = converttolocalparameter(listname); } private storablelist<double> _der;
if understand correct (and java), use reflection
field field = myclass.class.getdeclaredfield(lparam); field.set(this, values);
but if have limited amount of such values use few if's in method set value
edit: if it's c#, can read reflection in c#
Comments
Post a Comment