I asked this question last week about how to manually serialize objects, and I have since been trying to make generic wrapper that will solve my problem. (I'm doing it this way since I'm stuck between 3rd party code.) I pretty much have everything working except for the events.
Since I am technically creating a new instance of the object when I deserialize, I no longer retain my event subscriptions. Is there a way to copy over the subscriptions, or forward all events of the deserialized version back to the original version?
Here is something similar to what I am currently using:
[Serializable]
public class Wrapper<T> : ISerializable
{
public T Wrappee { get; set; }
public Wrapper(T wrappee)
{
Wrappee = wrappee;
}
protected Wrapper(SerializationInfo info, StreamingContext context)
{
Wrappee = (T)FormatterServices.GetUninitializedObject(typeof(T));
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo fieldInfo in fields)
{
var value = info.GetValue(fieldInfo.Name, fieldInfo.FieldType);
fieldInfo.SetValue(Wrappee, value);
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
FieldInfo[] fields = typeof(T).GetFields();
foreach (FieldInfo fieldInfo in fields)
{
var value = fieldInfo.GetValue(Wrappee);
info.AddValue(fieldInfo.Name, value);
}
}
}
Well that's just the general idea of it. Basically I was wondering if there was a way to do something with the events similar to what I did for the fields. I know I can get the EventInfo the same way, but I don't know how this would translate to fixing the subscriptions.
Any help would be greatly appreciated.
Edit So I am still trying to find a way to do this, and I came across this question. Would I be able to do something like this, and then forward the events back to the original appdomain without having to fix the subscriptions on that end?
The question is related to the question of how object graphs can be serialized. An event holds references to objects plus references to methods of these objects. How could object graphs be serialized?
I would give each object to be serialized a unique ID. If you do not want to store this ID in the objects, you can store them in a Dictionary<TObject, TID>
. In a first pass generate the IDs and add them to the dictionary. In a second pass serialize the objects together with their IDs. Instead of references to other objects, serialize the IDs of the related objects that you can find in the dictionary.
For events, store a method name in addition to the referenced object's ID.
When de-serializing the objects, you will need a reverse dictionary: Dictionary<TID, TObject>
. In a first pass de-serialize the objects and add them to the dictionary keyed by their IDs. Also add the de-serialized objects to a list. In a second pass go through the list and fix the references and events.
I hope this gives you an idea of a possible procedure.
You can get the event info with the GetInvocationList
method
class ClassWithEvent
{
public event EventHandler SomeEvent;
public Delegate[] GetSomeEventInvocationList()
{
return SomeEvent.GetInvocationList();
}
}
I tested the method with
class Subscriber
{
public void SomeMethod(object sender, EventArgs e)
{
}
}
static class Serialize
{
public static void Test()
{
var objWithEvent = new ClassWithEvent();
var subscriber1 = new Subscriber();
var subscriber2 = new Subscriber();
objWithEvent.SomeEvent += subscriber1.SomeMethod;
objWithEvent.SomeEvent += subscriber2.SomeMethod;
Delegate[] eventInfo = objWithEvent.GetSomeEventInvocationList();
foreach (Delegate d in eventInfo) {
Console.WriteLine("Target = {0}, Method = {1}",
d.Target, d.Method.Name);
}
}
}
Calling Serialize.Test();
outputs this in the console
Target = StackOverflowTests.SerializeEvent.Subscriber, Method = SomeMethod
Target = StackOverflowTests.SerializeEvent.Subscriber, Method = SomeMethod