c# - Store reference to an object in dictionary -
c# - Store reference to an object in dictionary -
i have been searching way save references of variables of various types dictionary, corresponding key. modify instance of variable accessing reference through dictionary key. storing references, tried utilize <object>
, without success. neither dictionaries nor lists take dictionary<string, ref int>
. next code compiles, seems update variables value only. ideas or workarounds?
here's (tested) code:
class test1 { idictionary<string, object> mydict = new dictionary<string, object>(); public void savevar(string key, ref int v) //storing ref int { mydict.add(key, v); } public void savevar(string key, ref string s) //storing ref string { mydict.add(key, s); } public void changevar(string key) //changing of them { if(mydict.gettype() == typeof(int)) { mydict[key] = (int)mydict[key] * 2; } if(mydict.gettype() == typeof(string)) { mydict[key] = "hello"; } } }
and how phone call methods of class
test1 t1 = new test1(); int myint = 3; string mystring = "defaultstring"; console.writeline(myint); //returns "3" console.writeline(mystring); //returns "defaultstring" t1.savevar("key1", ref myint); t1.savevar("key2", ref mystring); t1.changevar("key1"); t1.changevar("key2"); console.writeline(myint); //should homecoming "6" console.writeline(mystring); //should homecoming "hello"
the best solution can think of store delegates in dictionary allow retrieve , modify variables.
let’s start declaring type contains getter , setter delegate:
sealed class variablereference { public func<object> { get; private set; } public action<object> set { get; private set; } public variablereference(func<object> getter, action<object> setter) { = getter; set = setter; } }
the dictionary have type:
dictionary<string, variablereference>
to store variable, foo
of type string
, in dictionary, you’d write following:
mydic.add(key, new variablereference( () => foo, // getter val => { foo = (string) val; } // setter ));
to retrieve value of variable, you’d write
var value = mydic[key].get();
to change value of variable newvalue
, you’d write
mydic[key].set(newvalue);
this way, variable you’re changing genuinely original variable foo
, , foo
can (a local variable, parameter, field on object, static field... property).
putting together, class test1
like:
class test1 { dictionary<string, variablereference> mydict = new dictionary<string, variablereference>(); public void savevar(string key, func<object> getter, action<object> setter) { mydict.add(key, new variablereference(getter, setter)); } public void changevar(string key) // changing of them { if (mydict[key].get() int) { mydict[key].set((int)mydict[key].get() * 2); } else if (mydict[key].get() string) { mydict[key].set("hello"); } } } // ... test1 t1 = new test1(); int myint = 3; string mystring = "defaultstring"; console.writeline(myint); // prints "3" console.writeline(mystring); // prints "defaultstring" t1.savevar("key1", () => myint, v => { myint = (int) v; }); t1.savevar("key2", () => mystring, v => { mystring = (string) v; }); t1.changevar("key1"); t1.changevar("key2"); console.writeline(myint); // prints "6" console.writeline(mystring); // prints "hello"
c# object dictionary reference
Comments
Post a Comment