Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

.net - How to Attach the Events of an Original Object to a Deep Copied Clone

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...