0

I know [Serializable] marks a type to be serializable and ISerializable makes you can do custom serialization/deserialization by implementing GetObjectData, so you can see code like this below:

[Serializable]
public class MyType : ISerializable {
   ...
   void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { ... }
}

My question is, when a type implements ISerializable, it implicitly indicates this type is "serializable", because the logic is, if you can have control over doing a thing as you wish, then you are "capable" to do this thing, so I think it isn't it better to omit [Serializable] when a type implements ISerializable ?

  • More info [here](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2237), and ultimately [here](https://duckduckgo.com/?q=serializable+vs+iserializable). – Andy May 07 '21 at 03:47
  • Side note, in 2021 you should try to avoid both. Don't use it in new code. – H H May 07 '21 at 05:57

1 Answers1

1

Yes omit it! With ISerializable you can define custom sterilizing behavior through the GetObjectData override. Using the attribute is better for single properties on an fields. Here's a more detailed example of each use this example of a custom serialization here on StackOverflow