Reputation: 3256
I am attempting to serialize a class with a EventHandler using protobuf-net, using the following code:
[ProtoContract]
class Thing
{
[ProtoMember(5, AsReference = true)]
public EventHandler _DoSomething;
public event EventHandler DoSomething
{
add { _DoSomething += value; }
remove { _DoSomething -= value; }
}
public void PerformSomething(object sender, EventArgs args)
{
}
}
[TestMethod]
public void SerializingAClassWithAnEvent_Deserializes()
{
var Guy1 = new Thing() {};
var Guy2 = new Thing() {};
Guy2.DoSomething += Guy1.PerformSomething;
Assert.IsNotNull(Guy2._DoSomething);
MemoryStream buffer = new MemoryStream();
Serializer.Serialize(buffer, Guy2);
MemoryStream afterStream = new MemoryStream(buffer.ToArray());
var outGuy = Serializer.Deserialize<Thing>(afterStream);
Assert.IsNotNull(outGuy._DoSomething);
}
This code compiles and runs fine but the second assertion fails because the _DoSomething EventHandler is still null. What am I missing?
Upvotes: 4
Views: 617
Reputation: 3996
From the documentation here EventHandlers are not supported
custom classes that:
are marked as data-contract;
have a parameterless constructor;
for Silverlight: are public
many common primitives etc
single dimension arrays: T[]
List<T> / IList<T>
Dictionary<TKey,TValue> / IDictionary<TKey,TValue>
any type which implements IEnumerable<T> and has an Add(T) method
Upvotes: 4