CA2229: Implement serialization constructors
Collapse the table of content
Expand the table of content

CA2229: Implement serialization constructors








Breaking Change

Non Breaking

The type implements the System.Runtime.Serialization.ISerializable interface, is not a delegate or interface, and one of the following conditions is true:

This rule is relevant for types that support custom serialization. A type supports custom serialization if it implements the ISerializable interface. The serialization constructor is required to deserialize, or re-create objects that have been serialized using the ISerializable.GetObjectData method.

To fix a violation of this rule, implement the serialization constructor. For a sealed class, make the constructor private; otherwise, make it protected.

Do not suppress a violation of the rule. The type will not be deserializable, and will not function in many scenarios.

The following example shows a type that satisfies the rule.

using System;

namespace Samples1
    public class Book    
        private readonly string _Title;

        public Book(string title)        
            // Violates this rule (constructor arguments are switched)            
            if (title == null)                
                throw new ArgumentNullException("title cannot be a null reference (Nothing in Visual Basic)", "title");

            _Title = title;        

        public string Title        
            get { return _Title; }        
© 2015 Microsoft