如何将原始对象的事件附加到深度复制的克隆 [英] How to Attach the Events of an Original Object to a Deep Copied Clone
问题描述
昨天跟着我的问题,在C#中对事件进行深入复制,并将原始对象的事件附加到克隆拷贝是非常简单的,您只需将Copy中的事件声明设置为原始值中的值即可。 事件附加后深层克隆
Following up on my question yesterday to deepcopy an object with events in C# and attach the events of the original object to the Cloned copy is pretty easy, you just set the Event declaration in the Copy = the value in the original. Deep Clone when events are attached
如何在VB.Net中这样做? (使用.Net 2)
How do you do this in VB.Net? (Using .Net 2)
我希望也许有反思的东西,你可以检查什么事件被绑定,并以某种方式将它们转移到新对象。 >
I was hoping maybe there was something with reflection where you can examine what events are bound and somehow transfer those to the new object.
推荐答案
是的,你可以,而不是那么困难,但是似乎有很多关于这个的信息,这么大的问题。 / p>
yes you can, and its not that difficult, but it seems there isint a whole lot of info on this, so great question.
Dim sourceObject As New FooBar
Dim destObject As New FooBar
AddHandler sourceObject.SomeEvent, AddressOf myFunc
Dim miHandler As FieldInfo = GetType(FooBar).GetField("SomeEvent", BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Instance)
Dim sourceDelegate As [Delegate] = miHandler.GetValue(sourceObject)
Dim addDelegate As [Delegate] = sourceDelegate.GetInvocationList().First() ' if its multicast, then you'll need to copy the lot
AddHandler destObject.SomeEvent, addDelegate
这篇关于如何将原始对象的事件附加到深度复制的克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!