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

后端 未结 1 1698
北恋
北恋 2020-12-10 07:26

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 th

相关标签:
1条回答
  • 2020-12-10 08:05

    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
    
    
    0 讨论(0)
提交回复
热议问题