c# - Get value of non-existing variables by calling a method -
as strange might sound, want access not.
here's example:
int tempvar = myobject.myvar // myvar not exist;
in reality want class run method, take non-existing variable parameter:
object returningvariables(string variablename) { object desiredobject; // concrete object have stored somewhere in array inside class // going trough list contains names of variables, when finds // return return desiredobject; }
i'll try explain in concrete example want achieve , why.
i have class stores stats of object: name, level, speed, size, etc. supposed accessed trough method statdata getstat(string statname)
;
stats created trough method void makestat()
. problem began when stats created in class public variables , accessed other classed not trough getstat() method.
now public variables changed, deleted or new ones added takes refactoring app working again. , looks bad when there mixed calls direct access public variable , method same thing.
please note, know how fix code standard way; merely interested if above described technique work.
sounds dynamicobject here. can override different methods of class whatever being called/accessed on object: members, invocations, etc. using dynamic means won't able use intellisense anymore however. can find more info here.
public class mydynamicobject : dynamicobject { public override bool trygetmember(getmemberbinder binder, out object result){ if (binder.name == "myvar"){ result = "xyz"; return true; } result = null; return false; } } // usage dynamic x = new mydynamicobject(); console.writeline (x.myvar); // output "xyz"
Comments
Post a Comment