Avatar of Stafford Martin
Stafford Martin
Flag for United States of America asked on

How do I transfer associated callback methods with events in one object to the same event in another object of the same class?

I have a class, let's call it class1.   class1 contains an event, event1.    Consider this code snippet:


class1 cls = new class1();

class1 copy = cls.Clone();


The Clone method does what its name suggests, it clones the object cls.   I want the callback method associated the event event1 in cls to be associated with the event event1 in copy.  In other words, I want to clone (transfer) the association so that when event1 fires in copy, the same callback method is associated with cls is invoked.  


How can  I do that?

C#

Avatar of undefined
Last Comment
Stafford Martin

8/22/2022 - Mon
Kyle Abrahams

2 ways of doing it:

If you own the class, you can assign the event to the new event in the clone method.

If you don't own the class, use reflection.

From:  https://stackoverflow.com/questions/6055038/how-to-clone-control-event-handlers-at-run-time
var eventsField = typeof(Component).GetField("events", BindingFlags.NonPublic | BindingFlags.Instance);
var eventHandlerList = eventsField.GetValue(button1);
eventsField.SetValue(button2, eventHandlerList);

Open in new window

Stafford Martin

ASKER
(a) Doesn't apply.  Simple assignments cannot be done between events.
         
      copy.even1 = cls.event1;

     throws a syntax error.

(b) This solution only works when class class1 is a control in a Winform.  I need a solution for any arbitrary class in which an event is defined.

Kyle Abrahams

Reflection should work.

If not just an equal, the += should work.

The equal is if you are assigning from inside of the class when you clone it.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Stafford Martin

ASKER
I am sorry, += doesn't work either.  The right-hand side of that operator must be an Eventhandler object.  But I don't know the name of the callback method associated with the object cls. I know that reflection should work, but I have not been able to work it out.  If there is a use of Reflection that would give me a pointer/name to the callback method associated with cls, that would solve the problem.

Kyle Abrahams

What platform are you using?

What Class is cls?

Do you have access to the clone event?  If not can you inherit from the cls to make your own custom class?

What's the overall purpose of this?
Stafford Martin

ASKER
What platform are you using?    Windows 11  (VS 2022)

What Class is cls?  A user-defined class. I want to be able to do for any class I define.

Do you have access to the clone event?   There isn't a clone event.  There is a Clone() method and I want to be able to clone the events for the class.

If not can you inherit from the cls to make your own custom class?  I can inherit the event.  That is not the issue.  That is at the class definition level.  I want to be able to pass on associated callback methods.  There is no problem with inheriting events.

What's the overall purpose of this?  The specific purpose will depend on the class.  I am looking for a general approach like option (2) in the first response I got for this question.  That had the right approach except that it only worked for Winform controls.

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
louisfr

Inside the Clone method, you should be able to copy the delegate for the event.
copy.event = this.event;

Open in new window

Stafford Martin

ASKER
No. You can't.  Even though I knew it was incorrect the first time it was suggested, I tried it anyway. The results were as I expected.  I got a syntax error;

An event only allows the assignment operators  += and -=.  And the right hand must be EventHandler, not an event.



Regards

ASKER CERTIFIED SOLUTION
louisfr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Stafford Martin

ASKER
Ok.  It works inside the class.

I have been seldom as happy to be wrong.  Thanks!
Your help has saved me hundreds of hours of internet surfing.
fblack61