views:

235

answers:

4

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.

A: 

Hi,

Probably you need to expose those property implicitly within your class:

instead of IModel.Model property add

public MyClass Model
Vitaliy Liptchinsky
Yes, but in my case I can't do that, I was hoping to solve that problem without doing this.
dr. evil
+4  A: 

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:

Testcase error, it works on my computer.


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?

Henk Holterman
This would mean that the Interface needs to implement the Seriaizable Interface as well ;-) which makes perfect sense to me.
weismat
Sure it does and I agree on that, the question is how to solve it? :)
dr. evil
I asked the question badly though, I'll change the title.
dr. evil
It doesn't make sense for binary serialization, binary serialization serializes the concrete type also, so if and IModel points to a Model object the serialized type information will contain enough info to deserialize a Model object back.
Pop Catalin
Actually @Pop is right, it actually knows concrete type of the object, so should able to deseriliaze it
dr. evil
Wow, good question, I'm not sure. I overly simplified the question, I've got several interfaces and inner classes but after all this is the same theory. I'm not quite sure why I'm getting the error when this example works just fine.
dr. evil
+1 Thanks for the clarification, this changes everything :) I'll try to find what's different with your example and mine.
dr. evil
What was the difference?
Andy Johnson
A: 

I'm fairly sure that you can't serialize/deserialize interfaces, only instances of classes. I can't find any documentation to confirm this, but I recall trying to do the same thing without any success.

You could try using an abstract base class instead of an interface, but I haven't tried that.

Andy Johnson
I'm trying to serilize an instance not the interface itself. It's a type which implements this interface and referenced within the seriliazed class.
dr. evil
Ok. I wasn't being precise enough. I meant "you can't serialize/deserialize an object using a reference to an interface that it implements". I'm starting to think the problem that I had is not the same as the problem you're having.
Andy Johnson
Question: You say that your class has a property called Model. Later you say that "Model marked as serializable". Are you referring there to a class named Model or the property named Model? Are the classes that implement IModel all attributed as [Serializable]?
Andy Johnson
+1  A: 

I suspect it's because during deserialization it initiates a new instance of the class and then copies the data into it. It can't new an interface so the deserialization can not be completed. This is why you need a constructor that takes no arguments for serialization.

Not sure on the solution, I've never worked that bit out. I'd probably override the class and inherit the property with a concrete type then serialize that.

L2Type