我有一个从上下文绑定对象继承的类。类具有某些属性的属性。当某些属性发生更改时,PostProcess(IMessage msg, IMessage msgReturn)
引发一个事件,并再次从该事件中触发具有相同属性的新属性。第二个更改也应该调用PostProcess
,但它没有发生。可能是因为,更改第二个属性的对象不是原始 .net 对象,而是MarshalByRefObject / ContextBoundObject / Proxy Object
. 我的查询是如何将代理转换为原始对象。我尝试铸造 and SynchonizationAttribute
,但它没有帮助。只是为了让您知道事件正在以某种Async
方式执行,因此它不会阻止代码执行,并且代理和原始对象都存在于同一个应用程序域中。
我尝试使用两个对象,一个持有第二个的引用,当第一个的属性发生更改时,它尝试更改第二个的属性,但它没有调用PostProcess
.
基本上我需要制作一棵树,其中各种对象取决于其他对象的属性。并且当任何一个属性发生变化时,它都应该触发它的观察者,这可以像链一样传播,直到找不到观察者。我正在尝试使用 ContextBoundObject。
样本:
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 be called after this, but it is 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);
});
}
}
主要方法
PowerSwitch s1 = new PowerSwitch();
Bulb b1 = new Bulb();
b1.Watch(s1, "IsOn");
s1.IsOn = true; //<-- b1.update is called after this
请提出替代或更好的方法来实现我想要实现的目标。