如何在对象上实现更改通知

发布于 2024-10-09 11:24:40 字数 505 浏览 4 评论 0 原文

我有一个普通的普通类,例如:

public class ObjectA
{
    public string val {get;set;}
    ...
}

在另一个类中,它保存一个 ObjectA 的实例,例如:

public class ObjectB
{
     private ObjectA objectA;
     ....
}

实例“objectA”将经常更改。 我的意思是在 ObjectB 中,某些方法将对象 A 的新实例并分配给“objectA”,

是否有一种方法可以实现触发器,每当实例 objectA 发生更改时,将允许我执行以下操作某事,例如:

objectA += OnChanged_ObjectA   

protected void OnOnChanged_ObjectA()
{
    // do something
}

I have a plain normal class, e.g.:

public class ObjectA
{
    public string val {get;set;}
    ...
}

in another class, it holds an instance of ObjectA, e.g.:

public class ObjectB
{
     private ObjectA objectA;
     ....
}

the instance "objectA" will be frequently changed.
i mean in ObjectB, some of the method will new and new instance of Object A and assign to "objectA"

is there a way to implement a trigger, whenever instance objectA is change, will allow me to do something, e.g.:

objectA += OnChanged_ObjectA   

protected void OnOnChanged_ObjectA()
{
    // do something
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无戏配角 2024-10-16 11:24:40

您可以在 ObjectA 上创建更改事件您将触发您希望跟踪的任何更改。这将允许您在任何其他对象(包括 ObjectB)上订阅此事件。

Object1.cs中:

// Delegate type for the event handler
public delegate void MyEventHandler();

// Declare the event.
public event MyEventHandler MyEvent;

// In the properties or any place you what to notify of change:
if (MyEvent != null)
      MyEvent();

Object2.cs中您可以订阅:

objectA.MyEvent += OnChanged_ObjectA


protected void OnOnChanged_ObjectA()
{
    // Action changes
}

You can create a change event on ObjectA that you will fire for any of the changes you wish to track. This will allow you to subscribe to this event on any other object, including ObjectB.

In Object1.cs:

// Delegate type for the event handler
public delegate void MyEventHandler();

// Declare the event.
public event MyEventHandler MyEvent;

// In the properties or any place you what to notify of change:
if (MyEvent != null)
      MyEvent();

And in Object2.cs you can subscribe:

objectA.MyEvent += OnChanged_ObjectA


protected void OnOnChanged_ObjectA()
{
    // Action changes
}
滥情哥ㄟ 2024-10-16 11:24:40

我最近遇到了这样的问题,但不幸的是,我没有找到任何其他解决方案,但是这个:

public ObjectB
{
    private ObjectA _objectA;
    public  ObjectA objectA
    {
        get 
        { 
            return _objectA; 
        }
        set
        {
            if (value != _objectA)
            {
                _objectA = value;
                RaiseObjectAChanged(/* sender, args */);
            }
        }
    }
    private RaiseObjectAChanged()
    {
        // raise event here
    }
    private OnObjectAChanged()
    {
        // event handler
    }
}

I had such problem recently, but unfortunately, I didn't find any another solution, but this:

public ObjectB
{
    private ObjectA _objectA;
    public  ObjectA objectA
    {
        get 
        { 
            return _objectA; 
        }
        set
        {
            if (value != _objectA)
            {
                _objectA = value;
                RaiseObjectAChanged(/* sender, args */);
            }
        }
    }
    private RaiseObjectAChanged()
    {
        // raise event here
    }
    private OnObjectAChanged()
    {
        // event handler
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文