I serialize a class which includes a property called Model
as IModel
but when I try to Deserialize it I'm getting the following exception:
System.Runtime.Serialization.SerializationException: Type 'MYN.IModel' in Assembly 'MYN.Defs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
It's binary serialization. Model
marked as serializable. Obviously IModel is not.
So what's the solution, what am I doing wrong? Why does it try to seriliaze or deserialize an interface anyway?
P.S. Interface hasn't got an Enum in it.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.
Serialization and deserialization in . NET application, JSON data format conversion to . NET objects and vice versa is very common. Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.
Deserialization is the process of reversing a String from a previously serialized format. This coverts the serialized String into a format that allows its Data Structure properties to be accessible to manipulation.
Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.
It makes sense to get that error, because an IModel
property could refer to different classes and there is no guarantee that they are all Serializable.
OK, I gave it a try and we have a:
interface IFoo { }
[Serializable]
class CFoo : IFoo { }
[Serializable]
class Bar
{
public IFoo Foo { get; set; }
}
And Bar Serializes and Deserializes fine.
Bar b = new Bar();
b.Foo = new CFoo();
using (var s = new System.IO.MemoryStream())
{
var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(s, b);
s.Position = 0;
b = (Bar)bf.Deserialize(s);
Console.WriteLine("OK");
}
So, what is different from your IModel
and Model
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With