事件订阅者克隆

发布于 2024-10-25 01:36:18 字数 910 浏览 3 评论 0原文

我想知道如何最好地克隆一个对象并将事件订阅者重新附加到新克隆的对象。

背景:我使用转换器,它可以从字符串转换为对象。该对象在转换器的上下文中是已知的,因此我只想获取该对象并复制属性值和事件调用列表:

[TypeConverter(typeof(MyConverter))]
class MyObject 
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public delegate void UpdateHandler(MyObject sender);
    public event UpdateHandler Updated;
}

class MyConverter(...) : ExpandableObjectConverter
{
    public override bool CanConvertFrom(...)
    public override object ConvertFrom(...) 
    {
        MyObject Copied = new MyObject();   
        Copied.prop1 = (value as string);
        Copied.prop2 = (value as string);

        // For easier understanding, let's assume I have access to the source
        // object by using the object named "Original":

        Copied.Updated += Original.???
    }

    return Copied;
}

那么当我有权访问源对象时,是否有可能将其订阅者附加到复制对象事件?

问候, 格雷格

I would like to know how it is best done to clone an object and reattach the event subscribers to the newly cloned object.

Background: I use a Converter, which can convert from a string to an object. The object is known in the context of the converter, so I just want to take that object and copy the property values and the event invocation lists:

[TypeConverter(typeof(MyConverter))]
class MyObject 
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public delegate void UpdateHandler(MyObject sender);
    public event UpdateHandler Updated;
}

class MyConverter(...) : ExpandableObjectConverter
{
    public override bool CanConvertFrom(...)
    public override object ConvertFrom(...) 
    {
        MyObject Copied = new MyObject();   
        Copied.prop1 = (value as string);
        Copied.prop2 = (value as string);

        // For easier understanding, let's assume I have access to the source
        // object by using the object named "Original":

        Copied.Updated += Original.???
    }

    return Copied;
}

So is there a possibility, when I have access to the source object, to attach its subscribers to the copied objects event?

Regards,
Greg

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

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

发布评论

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

评论(1

萧瑟寒风 2024-11-01 01:36:19

那么,您可以在原始中定义一个函数,为您提供事件处理程序。

原始类

class A
{
    public event EventHandler Event;

    public void Fire()
    {
        if (this.Event != null)
        {
            this.Event(this, new EventArgs());
        }
    }

    public EventHandler GetInvocationList()
    {
        return this.Event;
    }
}

然后从转换器中调用以下内容:

Copied.Event = Original.GetInvocationList();

Well you can define a function in Original class that gives you the event handlers.

Original Class:

class A
{
    public event EventHandler Event;

    public void Fire()
    {
        if (this.Event != null)
        {
            this.Event(this, new EventArgs());
        }
    }

    public EventHandler GetInvocationList()
    {
        return this.Event;
    }
}

And then call the following from your converter:

Copied.Event = Original.GetInvocationList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文