C # clone EventHandler

I have a class with several EventHandlers (by the way):

public GameObject { public event EventHandler<EventArgs> Initialize; public event EventHandler<EventArgs> BeginStep; .... } 

I want to add the Clone() function to GameObject, which returns an exact duplicate of the object it was called on. I tried to do it like this:

  public GameObject Clone() { var clone = new GameObject() { Initialize = this.Initialize, BeginStep = this.BeginStep, }; } 

But it looks like instead of this.BeginStep it does clone.BeginStep on the same object as this.BeginStep . So how do I make a copy of an EventHandler object?

+4
source share
5 answers

You do not need to worry about it. The EventHandler<EventArgs> object is immutable, so any change in the list of listeners in any object will cause this object to receive a new instance of EventHandler<EventArgs> containing an updated call list. This change will not be present in another GameObject .

+6
source

Try adding it using the + = operator. I did not even know that an event could be scheduled.

 clone.Initialize += this.Initialize; 

In addition, all delegates are immutable value types, so you don’t have to worry about pointing to the same object β€” when you perform the operation described above, the entire delegate is copied (cloned if you want).

+3
source

It depends on whether your events are delegated to the methods defined in the GameObject class, or whether they are passed to another instance of the observer class.

If events are processed in methods defined in your GameObject class, and you want events in the clone to be processed using methods in your clone instance, you can get a usage reflection to get information about the method from the original event handlers, create a new delegate using the cloned instance and method name, and then designate the new delegate as the cloned event handler.

  public GameObject Clone() { var clone = new GameObject(); foreach (var target in this.Initialize.GetInvocationList()) { var mi = target.Method; var del = Delegate.CreateDelegate( typeof(EventHandler<EventArgs>), clone, mi.Name); clone.Initialize += (EventHandler<EventArgs>)del; } return clone; } 

If the events are processed in another class, you do not need to do anything, but all event notifications for both the source instance and the cloned instance will have the same handlers. If this is not what you want, you will need to change the event delegates after cloning.

+1
source

You do not need to clone events, just as you do not need to clone any methods of the source object. When you clone, all you really need to duplicate is the member / property values.

0
source

You want to do something similar to what was published on Deep Cloning Objects

 public static GameObject Clone(GameObject source) { // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(GameObject); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (GameObject)formatter.Deserialize(stream); } } 

Your class must be serializable.

EDIT: As I said, this was based on the code I attached to, and I hastened to give an answer. Had to check it out a little closer.

0
source

All Articles