I use ProtoBuf to serialize class. Unfortunately Serializer.Deserialize method gives error “Type is not expected, and no contract can be inferred: System.Object”. Any hint how to solve this.
private void WriteToFile( string siteID, object objectTemp, stringfileSystemPath)
{
var type = objectTemp.GetType();
using (var file = File.Create(fileSystemPath))
{
Serializer.NonGeneric.Serialize(file, objectTemp);
}
}
private object ReadFromFile( string siteID, object objectTemp, stringfileSystemPath)
{
Type type = objectTemp.GetType();
object objectTemp2=null;
using (var file = File.OpenRead(fileSystemPath))
{
objectTemp2 = Serializer.NonGeneric.Deserialize(type, file);
}
return objectTemp2;
}
}
I am using protobuf.Net v2 beta with .Net 3.5
Serializer.NonGeneric.Deserializewants to be provided a protobuf-generated type. (you are giving it the type ofSystem.Object. It doesn’t know what to do with that).Look at the C# code generated by protoc, and specify a
typeof()of the class you are trying to read from file.Or put another way, if
WriteToFileis succeeding, then use the type of objectTemp. The type that went in to the file is the type that needs to come out of the file.