事件订阅者克隆
我想知道如何最好地克隆一个对象并将事件订阅者重新附加到新克隆的对象。
背景:我使用转换器,它可以从字符串转换为对象。该对象在转换器的上下文中是已知的,因此我只想获取该对象并复制属性值和事件调用列表:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您可以在原始
类
中定义一个函数,为您提供事件
处理程序。原始类:
然后从转换器中调用以下内容:
Well you can define a function in Original
class
that gives you theevent
handlers.Original Class:
And then call the following from your converter: