克隆对象事件。 [英] Clone objects events.

查看:80
本文介绍了克隆对象事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

什么是用事件克隆对象的最佳方法?


这是因为我已经对我的对象进行了深层复制并添加了新的事件

处理程序但事件是从原始实例触发的,而不是克隆的

实例。


谢谢。

Stefano。

解决方案

如果它是你自己的对象,那么只需复制委托即可。复制你不拥有类型的

事件非常棘手,而且可能不是很好的想法 - 尤其是因为平均来电者不会期望通过他们没有订阅的东西来定位

- 这可能会导致意外的内存泄漏*和未找到发件人的错误在预期的清单中。


Marc


* =不是真正的泄漏 - 只是一个物体的副作用保持另一个

alive。


2008年5月20日星期二01:04:45 -0700,Ste< sc ****** @ svilupso.itwrote :


早上好,

什么是用事件克隆对象的最佳方法?


这是因为我已经制作了我的对象的深层副本并添加了新事件

处理程序,但事件是从原始实例触发的,而不是克隆的

实例。



您是否实际克隆了作为订阅活动的

代表的目标的实例?或者你只是克隆代表

自己?你需要做前者,这当然意味着订阅者对象也需要实现ICloneable或

等价物。


如果这没有回答你的问题,你需要发一个简单但完整的代码示例来证明你的意思。


Pete


5月20日上午9:04,Ste< sceru ... @svilupso.itwrote:


早上好,

什么是用事件克隆对象的最佳方法?


这是因为我已经制作了一个深层副本我的对象并添加了新事件

处理程序,但事件是从原始实例触发的,而不是克隆的

实例。



我怀疑没有具体的例子我们很快就会遇到

的误解。你能发一个简短但完整的

计划来证明这个问题吗?请参阅 http://pobox.com/~skeet/csharp/complete.html

我的意思是什么。


对于它的价值,根据我的经验,克隆往往充满了< />
问题,我怀疑将代表引入混合将

只会让它更加如此。


Jon


Good morning,
what can be the best way to clone an object with events ?

This because I have made a deep copy of my object and added new event
handlers but events are fired from the original instance, not in cloned
instance.

Thank you.
Stefano.

解决方案

If it is your own object, then simply copy the delegate. Copying the
events of a type you don''t own is much tricker, and probably not a good
idea - not least because the average caller won''t expect to be targetted
by something they didn''t subscribe to - which could lead to unexpected
memory leaks* and bugs from not finding "sender" in an expected list.

Marc

*=not a true leak - just the side-effect of one object keeping another
alive.


On Tue, 20 May 2008 01:04:45 -0700, Ste <sc******@svilupso.itwrote:

Good morning,
what can be the best way to clone an object with events ?

This because I have made a deep copy of my object and added new event
handlers but events are fired from the original instance, not in cloned
instance.

Did you actually clone the instances that are the targets for the
delegates subscribed to the events? Or did you just clone the delegates
themselves? You would need to do the former, which of course means that
the subscriber objects would also need to implement ICloneable or the
equivalent.

If that doesn''t answer your question, you need to post a
concise-but-complete code sample that demonstrates what you mean.

Pete


On May 20, 9:04 am, Ste <sceru...@svilupso.itwrote:

Good morning,
what can be the best way to clone an object with events ?

This because I have made a deep copy of my object and added new event
handlers but events are fired from the original instance, not in cloned
instance.

I suspect that without a concrete example we could run into
misunderstandings very quickly. Could you post a short but complete
program demonstrating the problem? See http://pobox.com/~skeet/csharp/complete.html
for what I mean by that.

For what it''s worth, in my experience cloning tends to be fraught with
issues, and I suspect that introducing delegates into the mix will
only make it more so.

Jon


这篇关于克隆对象事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆