c# - Chain of events / Proxy to original object -
i have class inherited context bound object. class has attribute on properties. when property changed, postprocess(imessage msg, imessage msgreturn)
raise event , event again new property same attribute fired. second change should call postprocess
, not happening. because, object second property changed not original .net object marshalbyrefobject / contextboundobject / proxy object
. query how cast proxy original object. tried casting , synchonizationattribute
, not help. let know events executing in async
manner not block code execution, , both proxy , original object exist in same app domain.
i tried 2 object, 1 holding reference of second, , when property of first changed, try change property of second, did not invoke postprocess
.
basically need make tree various objects depending on property of other objects. , when 1 property changed, should trigger watcher, , spread chain untill no watcher found. trying contextboundobject.
sample:
public class powerswitch : objectbase { [watchable] public bool ison { get; set; } public bool isworking { get; set; } } public class bulb : objectbase { public color color { get; set; } [watchable] public bool ison { get; set; } protected override void update(object sender, propertychangeeventargs e) { ((bulb)this).ison = !((bulb)this).ison; //<-- b1.update should called after this, not } } [watchable] public class objectbase : contextboundobject { public virtual void watch(objectbase watch, string propertyname) { watch.watcher.add(this, propertyname); } protected virtual void update(object sender, propertychangeeventargs e) { } public dictionary<objectbase, string> watcher = new dictionary<objectbase, string>(); internal void notifywatcher( propertychangeeventargs propertychangeeventargs) { watcher.where(sk => sk.value == propertychangeeventargs.name) .tolist() .foreach((item) => { item.key.update(this, propertychangeeventargs); }); } }
main method
powerswitch s1 = new powerswitch(); bulb b1 = new bulb(); b1.watch(s1, "ison"); s1.ison = true; //<-- b1.update called after
please suggest alternate or better way implement want achieve.
it looks you're close observer pattern, observers subscribe status notifications on subject.
in pattern, b1.ison = true contain code cycles through observers , notifies them of changes. guess if wanted observe lot of properties on 1 object, encapsulate notification code. b1.ison have single line says like:
this.sendnotification("ison", value);
this seems lot you're doing... if read on pattern might end feeling little more confident, , maybe make few changes standardize code.
by way, there built .net -- iobservable(t). haven't used this, looks strong.
Comments
Post a Comment