-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Serialization.ImplementISerializableCorrectlyRule(git)
Assembly: Gendarme.Rules.Serialization
Version: git
This rule checks for types that implement ISerializable. Such types serialize their data by implementing GetObjectData. This rule verifies that every instance field, not decorated with the NonSerialized attribute is serialized by the GetObjectData method. This rule will also warn if the type is unsealed and the GetObjectData is not virtual.
Bad example:
[Serializable]
public class Bad : ISerializable {
int foo;
string bar;
protected Bad (SerializationInfo info, StreamingContext context)
{
foo = info.GetInt32 ("foo");
}
// extensibility is limited since GetObjectData is not virtual:
// any type inheriting won't be able to serialized additional fields
public void GetObjectData (SerializationInfo info, StreamingContext context)
{
info.AddValue ("foo", foo);
// 'bar' is not serialized, if not needed then the field should
// be decorated with [NotSerialized]
}
}
Good example (virtual and not serialized):
[Serializable]
public class Good : ISerializable {
int foo;
[NotSerialized]
string bar;
protected Good (SerializationInfo info, StreamingContext context)
{
foo = info.GetInt32 ("foo");
}
public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
{
info.AddValue ("foo", foo);
}
}
Good example (sealed type and serialized):
[Serializable]
public sealed class Good : ISerializable {
int foo;
string bar;
protected Good (SerializationInfo info, StreamingContext context)
{
foo = info.GetInt32 ("foo");
}
public void GetObjectData (SerializationInfo info, StreamingContext context)
{
info.AddValue ("foo", foo);
info.AddValue ("bar", bar);
}
}
- This rule is available since Gendarme 2.0
You can browse the latest source code of this rule on github.com
Note that this page was autogenerated (3/17/2011 1:55:44 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!