Gendarme.Rules.Serialization
From Mono
Gendarme's rules that check for serialization issues are located in the Gendarme.Rules.Serialization.dll assembly. Latest sources are available from anonymous SVN (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization/) (tarball (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Serialization.tar.gz?view=tar)).
| Table of contents |
Rules
DeserializeOptionalFieldRule
NOTE: At current time this rule is only available from our SVN repository. The next release of Gendarme will include this rule.
If your type has any field(s) marked with [OptionalField] then it should take care of re-computing this value when the data is deserialized using the [OnDeserialized] and [OnDeserializing] attributes.
Bad example:
[Serializable] public class ClassWithOptionalField { [OptionalField] private int optional; }
Good examples:
[Serializable] public class ClassWithOptionalField { [OptionalField] private int optional = 1; [OnDeserialized] private void Deserialized (StreamingContext context) { optional = 0; } [OnDeserializing] private void OnDeserializing (StreamingContext context) { optional = 0; } }
MissingSerializableAttributeOnISerializableTypeRule
NOTE: At current time this rule is only available from our SVN repository. The next release of Gendarme will include this rule.
Implementing ISerializable is not enough to make a class serializable as this interface only gives you more control over the basic serialization process. In order for the runtime to know your type is serializable it must have the [Serialization] pseudo-attribute.
Bad example:
public class MyType : ISerializable { }
Good examples:
[Serializable] public class MyType : ISerializable { }
MissingSerializationConstructorRule
NOTE: At current time this rule is only available from our SVN repository. The next release of Gendarme will include this rule.
Types that implements ISerializable must implement the serialization constructor but the interface alone cannot force you to do so. The serialization constructor should be private for sealed type, otherwise it should be protected.
Bad example:
[Serializable] public class ClassWithoutConstructor : ISerializable { public void GetObjectData (SerializationInfo info, StreamingContext context) { } }
Good examples:
[Serializable] public class ClassWithConstructor : ISerializable { protected ClassWithConstructor (SerializationInfo info, StreamingContext context) { } public void GetObjectData (SerializationInfo info, StreamingContext context) { } }
UseCorrectSignatureForSerializationMethodsRule
NOTE: At current time this rule is only available from our SVN repository. The next release of Gendarme will include this rule.
If you use the special serialization attributes, e.g. [OnSerializing, OnDeserializing, OnSerialized, OnDeserialized], you must ensure that the methods have the correct signature. They should be private, return void and have a single parameter of type StreamingContext. Failure to have the right signature can, in some circumstances, make your assembly unusable at runtime.
Bad example:
[Serializable] public class BadClass { [OnSerializing] public bool Serializing (StreamingContext context) { } }
Good examples:
[Serializable] public class BadClass { [OnSerializing] private void Serializing (StreamingContext context) { } }
Feedback
Please report any documentation errors, typos or suggestions to the Gendarme Google Group (http://groups.google.com/group/gendarme). Thanks!

Powered by MediaWiki