views:

217

answers:

4

I would think that adding that attribute to an interface would be helpful make sure you do not create classes that use the interface and forget to make them serializable.

This could be a very fundamental question, but I wanted to ask the experts.

+27  A: 

Interfaces define a contract and do not have any state of their own.

Serialization is about saving and loading state into and out of an object model.

Not much point to serializing something that holds no state.


To answer the practical question of forcing an implementation of an interface to be Serializable - this is why the ISerializable interface exists.

In .NET you can declare an interface that should implement other interfaces:

interface MustBeSerializable : ISerializable {}

See some more information here.

Oded
Awesome explanation :-)
Raja
Great explanation.
David Lively
Devil's advocate: interfaces imply *how* objects work with their state. Interfaces don't have state but they will be used to define objects that do and contracts with how the state can be accessed.
Dinah
nice job.......
jim
Dinah: The Serializable attribute has to do with the data not the behavior.
Klinger
+2  A: 

If you want to force classes that implement your custom interface IMyInterface to be serializable you can define it has:

interface IMyInterface : ISerializable
{
    // Custom interface definition
}

This more clearly indicates that the implementing class should support serialization. This does not remove the need to mark the class with the [Serializable] attribute.

IIRC, you can also create a FxCop custom rule that checks that classes that inherit from IMyInterface are marked with the respective [Serializable] attribute and this way removing the need to classes implement custom serialization.

João Angelo
A: 

There are some good albeit esoteric reasons behind what an interface is and isn't which keeps this from being possible. That said however: I agree with you. There are many things that would be useful if we could incorporate them into interfaces. [Serializable] and statics come to mind.

Although they do not fit into the philosophy of what an interface is, they seem to incorporate this vacant grey area in single-inheritance OOP. There are of course work arounds but they feel very forced compared to the original intent.

Dinah
A: 

Well, there is a reason that new classes are not marked as serializable by default: By adding the Serializable attribute, you acknowledge that you have ensured that serialization in your class works, by choosing proper data types for your instance fields and by adding serialization logic, if necessary.

So, if you "forgot" to add the Serializable attribute to your class, you most probably also forgot to check whether serialization really works on your class. Granted, in many cases it will work "out of the box", so adding the attribute is all that remains, but you are supposed to double-check and explicitly acknowledge that fact (by manually adding the attribute).

Heinzi