如何连接原始对象的事件了深刻的复制克隆 [英] How to Attach the Events of an Original Object to a Deep Copied Clone
问题描述
继昨天在我的问题的DeepCopy一个对象,在C#中的事件和连接到克隆拷贝原始对象的事件是pretty的方便,你只要设置事件声明的副本=的价值原版的。 <一href="http://stackoverflow.com/questions/4029600/performing-a-deepcopyclone-when-events-are-attached">Deep克隆事件时附加
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.
推荐答案
是的就可以了,它并不难,但似乎有isint一大堆关于这个信息,如此之大的问题。
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屋!