Innkeeper
Innkeeper

Reputation: 673

What mistake am I making when serializing?

This leads to serialization exception at runtime. It's just a demo project to test the best way to do this. I included the main method and the class which im trying to serialize.

Ignore: I really cant add more detail, i've described the problem, attached the code, this "please add more details" thing is the stupidest thing ever. Let me post it already.

Data toSend = new Data();
toSend.Output();

///SERIALIZE

BinaryFormatter formatter = new BinaryFormatter();
Stream streamOut = File.OpenWrite("file");
formatter.Serialize(streamOut, toSend);
streamOut.Close();


Console.WriteLine("----------------------------");
///DESERIALIZE

Stream streamIn = File.OpenRead("file");
Object received = formatter.Deserialize(streamIn);
Data toReceive = (Data)received;
toReceive.Output();

class Data : ISerializable
{
    int integerData;
    string stringData;
    bool booleanData;
    int shouldnotbeserialized;

    public Data()
    {
        integerData = 1;
        stringData = "Hello";
        booleanData = true;
        shouldnotbeserialized = 55;
    }

    //To deserialize
    public Data(SerializationInfo info, StreamingContext context)
    {
        integerData = info.GetInt32("theint");
        stringData = info.GetString("thestring");
        booleanData = info.GetBoolean("thebool");
    }

    public void Output()
    {
        Console.WriteLine(integerData);
        Console.WriteLine(stringData);
        Console.WriteLine(booleanData);
        Console.WriteLine(shouldnotbeserialized);
    }

    //Implemented method to serialize
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("thestring", stringData);
        info.AddValue("theint", integerData);
        info.AddValue("thebool", booleanData);
    }
}

Exception message:

Type 'SerializationDemo.Data' in Assembly 'SerializationDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Upvotes: 3

Views: 16252

Answers (1)

User 12345678
User 12345678

Reputation: 7804

The answer is given to you in the exception message:

Type 'SerializationDemo.Data' in Assembly 'SerializationDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

You need to mark your class with the Serializable attribute.

[Serializable()]
class Data : ISerializable

From the context it seems like you are going to be transmitting the object down a network (deduced due to the local variable names toSend, toReceive). You need to be aware that, if you where for example, using a client-server model, an object serialized and sent from the server software will not be deserializable from the client software by default.

This is because the fundamental characteristic of binary serialization is that it preserves type system fidelity. Type system fidelity denotes that type information is not lost in the serialization process. This means that when you serialize an object, is is not only the "object's state" written to the data stream, but also the name of the class and the containing assembly. Even if you defined the class Data in the assembly that is going to deserialize the data, the deserialize method will fail (throw an exception) because the deserializer will be looking for the type Program1.Namespace.Data not Program2.Namespace.Data. To remedy this you can define your data messages in a mutual assembly (class library) or by defining a SerializationBinder that will allow you to basically trick the deserialize method into thinking you are still in the same assembly.

Upvotes: 9

Related Questions