c# - How to extend a type with an onChange event -


specifically want add onchange event string class, how can that?

i have dictionary of strings member of 1 of classes. 1 or more of these strings might have functions need called when change. function defined code using class.

if it's possible, simplest way seems to add event handler string or make new class inherits string , have dictionary hold them. how this?

update:

my dictionary of strings, can change int or dynamic without fuss.

strings sealed. can not inherit them. in case, immutable, , can never changed. closest thing can using inotifypropertychanged subscribe property changes in object. in example, create listener gets updated when 1 of property values in foo changes.

void main() {     var f = new foo();     f.propertychanged += (sender, e) =>          console.writeline ("changed: {0}", e.propertyname);      f.name = "asdf";     f.size = 123; }  class foo     : system.componentmodel.inotifypropertychanged {     public event system.componentmodel.propertychangedeventhandler propertychanged;      private string _name;     public string name {         {             return this._name;         }         set {             if (this.propertychanged != null) {                 this.propertychanged(this, new system.componentmodel.propertychangedeventargs("name"));             }             this._name = value;         }     }      private int _size;     public int size {         {             return this._size;         }         set {             if (this.propertychanged != null) {                 this.propertychanged(this, new system.componentmodel.propertychangedeventargs("size"));             }             this._size = value;         }     } } 

output:

changed: name changed: size 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -