Brady Gaster: new FxCop rule – serializable classes need default constructors


In an earlier post, I talked about the different people creating FxCop rules. Brady Gaster, the impetus for that post has created another. Keep ’em coming, Brady!

fxcop rule : serializable classes need default constructors

with a little work and a lot of help from the guys over on the GDN FxCop messageboard, i’m able to publish this next rule. it does something like the one from yesterday, in terms of enforcing one of the most important serialization requirements imposed on us by the framework. here’s the deal – like yesterday’s rule, the serialization functionality within the framework needs to be able to create objects using a few assumptions. one of these assumptions is that serializable objects will be creatable without any construction parameters. so this rule basically enforces that requirement by ensuring that all objects marked with the Serializable attribute contain default public (instance-only!) constructors.

Comments (3)

  1. Michael says:

    I’m a bit confused by this rule. As I understand it:

    A default public instance constructor is only required for classes using xml serialization (System.Xml.Serialization).

    The SerializableAttribute is only required for classes using the serialization framework (System.Runtime.Serialization).

    It seems to me this rule is incorrect. Anyone agree?

  2. brady gaster says:

    this rule is designed to enforce the first rule in your list. the second isn’t considered.

  3. Michael says:

    The part that concerned me was this quote from blog: "so this rule basically enforces that requirement by ensuring that all objects marked with the Serializable attribute contain default public (instance-only!) constructors."