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?