I've been struggling with the exception 'No parameterless constructor found for [type]' for the past few hours. Now I created a simple unit test which should mirror what I've got in the application and it seems that this exception is thrown when I don't do stream.Position = 0.
Also, I don't get this exception in any case when the object is just a standard class (not derived from an abstract one).
See the code below:
run it as is - it will break saying that no constructor could be found for Base
uncomment stream.Position = 0 and it will be fine
comment the line out again, change the Derived class to not inherit from Base and uncomment the only property in the class, run it - it won't break (but obviously the Name will be null)
Can someone explain why this works this way ? Why #1 throws (or why #3 doesn't) and why this message?
[Test]
public void CanSerialize_Derived()
{
var derived = new Derived() {Name = "ngf"};
var stream = new MemoryStream();
Serializer.Serialize(stream, derived);
//stream.Position = 0;
var deserializedInstance = Serializer.Deserialize<Derived>(stream);
}
[ProtoContract]
[ProtoInclude(9, typeof(Derived))]
public abstract class Base
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
public class Derived : Base
{
//[ProtoMember(1)]
//public string Name { get; set; }
}