hoping someone can help me with this. Sorry if my technical language is imprecise, everything I know about this is self-taught.
I'm working on my own homebrew C# game engine project that uses serialization to achieve game saves. It's a solution I've been using for a while now and it works great. A problem when I first implemented this system was that some interactive objects call arbitrary methods via delegates (anonymous functions) that might be "added" to the object at runtime. I was a little intimidated by the prospect of serializing these along with the objects because I hear that what is actually being stored is a pointer/reference to the method that is called, and this is not guaranteed to be the same method on deserialization as it was when it was serialized, such is my understanding. While I'm not precisely sure of how these interactions work, I side-stepped the issue by instead storing a string which contains a reference key to what the delegate was doing and essentially "re-creating" the method upon deserialization.
I'm refactoring my code at the moment to make interactions between objects more fully leverage events. I'm on the verge of adding a similar system for event handlers, where when an object adds an event handler and becomes a subscriber to a given event, the object will also make a record so that it can resubscribe with the same functionality to the event when I deserialize the object. I'm fairly sure that this will work, but I'm realizing I'm doing a lot of work on a problem I'm not sure I even fully understand, because I'm not sure how the reference to the event handler or anonymous method is "stored" within an object in the first place.
For instance, say I decided to serialize all my events and delegates along with the rest of the object that they are attached to. Now say that an event handler is a method defined in code. Is the problem here that if I loaded the serialized object, it would run the latest version of the method if I had changed it since the object was serialized (not a problem for me), or that the reference to the method would now be pointing to an unrelated piece of data in the application memory (a huge problem!).
Now say that I have two objects, and one subscribes to the event of another with its own anonymous delegate/event handler "created" at runtime. Now say I serialize and deserialize both objects. Because both objects exist, would the delegate/event handler still have the correct "method instance" that was created in the other object, because the other object "still exists"? Or would it just contain a memory reference that is unrelated to the newly deserialized object (which could be being stored in an unrelated memory location to where it was originally)?
Basically, I don't fully understand how a program keeps track of its method definitions, and what exactly is being "saved" when one is serialized. Is the "reference" being looked up a specific method reference/name, or just a contextless piece of memory (which is the assumption I've been working under so far)? Is the position in memory where methods stored influenced by, say, how the memory of the program is utilized prior to an anonymous method being created (are these reference locations being created "on the fly" at runtime)?
I would appreciate further background so I know better if I'm coming at this problem from the right direction, and also any further advice with respect to handling event subscriptions and anonymous methods when serializing objects, in case anyone knows a substantially better approach to how I'm currently planning my application. I think that what I'm currently doing makes sense, but I want to be sure.
Thanks.