SerializableAtttribute vs ISerializable

897 views Asked by At

I have extension method

 public static T DeepClone<T>(this T source) where T : ISerializable
 {
  ..
 }

When I added "where T : ISerializable" I get error on all classes that uses DeepClone() having [Serializable] attribute.

  • What is the problem?

EDIT:

  • I know that my classes should implement ISerializable to work. But is there any difference in serialized output when using ISerializable vs [Serializable]?
4

There are 4 answers

0
decyclone On BEST ANSWER

There are two ways you can make a type serializable.

  • Implement ISerializable (Less used)
  • Apply [Serializable] attribute (More used and easier)

In your case, you only consider the first one.

To solve this, just let the serializer handle error reporting. It will throw error for any object that is not serializable.

0
Guillaume86 On

You must add the interface ISerializable to your classes

public class YourClass : ISerializable
0
Snowbear On

The problem is that assigning an attribute (Serializable) has nothing in common with implementing an interface (ISerializable).

0
Chris On

Just thought I would a little bit more to this answer. The [Serializable] vs ISerializable was something that confused me for a while and I finally got it straightened out.

When you want a class to be serializable, you apply the [Serializable] attribute. That's all you needed (and that is true today). However, there are times where you need finer control of the serialization process (maybe the receiver of the serialized object has some strict requirements that your object is less concerned with). To deal with any customization of how the object gets serialized in the pre-.NET 2.0 days, you implemented the ISerializable interface in addition to applying the [Serializable] attribute.

The ISerializable interface has a single function requirement:

void GetObjectData(SerializationInfo info,StreamingContext context)

In this function you serialized your content:

GetObjectData(SerializationInfo info,StreamingContext context)
{
   // example of some type of customization of the serialization process, giving
   // your serialized class member a custom name
   info.AddValue("GiveMyDataMemberCustomNameInOutput", myDataMember);
   ...

With the advent (.NET 2.0) of some additional [Serializable] attributes, you don't need to implement ISerializable.GetObjectData() in order to customize how something was serialized.

So how do you handle funky serialization the newer way? Well [Serializable] has some partners, notably [OnSerializing] and [OnDeserialized]. If you need to do the funky serialization/deserialization that you used to do with ISerializable.GetObjectData() you now handle it in events.

Two things I want to point out:

  1. There's not much of a reason (IMO, but could be edge cases) to use/implement the ISerializable interface. Just use the newer [Serializable] attributes
  2. If you feel you need ISerializable, then remember you must also apply the [Serializable] attribute to your class.