Skip to content

.Net Serialization Support

Cy Scott edited this page Sep 19, 2021 · 3 revisions

If an interface inherits the ISerializable interface then the void GetObjectData(SerializationInfo info, StreamingContext context) method will be implemented and a class constructor protected ClassNamehere(SerializationInfo info, StreamingContext context) will be generated. Here is a simple example of how it works:

[Generate]
public interface IHaveASimpleProperty : ISerializable
{
    DateTime DateTime { get; set; }
    Guid Id { get; set; }
    int Integer { get; set; }
    string String { get; set; }
}

The generated code will look like:

[System.Serializable]
public class HaveASimplePropertyModel : IHaveASimpleProperty
{
    public System.DateTime DateTime
    {
        get
        {
            return _DateTime;
        }
        set
        {
            _DateTime = value;
        }
    }
    private System.DateTime _DateTime;
        
    public System.Guid Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }
    private System.Guid _Id;
        
    public int Integer
    {
        get
        {
            return _Integer;
        }
        set
        {
            _Integer = value;
        }
    }
    private int _Integer;
        
    public string String
    {
        get
        {
            return _String;
        }
        set
        {
            _String = value;
        }
    }
    private string _String;
        
    public HaveASimplePropertyModel()
    {
    }
        
    /// <summary>
    /// Deserializes the class.
    /// </summary>
    protected HaveASimplePropertyModel(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
    {
        if (info == null) throw new System.ArgumentNullException("info");
            
        foreach (var it in info)
        {
            switch (it.Name)
            {
                case "DateTime":
                    this._DateTime = (System.DateTime)it.Value;
                    break;
                case "Id":
                    this._Id = (System.Guid)it.Value;
                    break;
                case "Integer":
                    this._Integer = (int)it.Value;
                    break;
                case "String":
                    this._String = (string)it.Value;
                    break;
            }
        }
    }
        
    void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
    {
        if (info == null) throw new System.ArgumentNullException("info");
            
        info.AddValue("DateTime", _DateTime, typeof(System.DateTime));
        info.AddValue("Id", _Id, typeof(System.Guid));
        info.AddValue("Integer", _Integer, typeof(int));
        info.AddValue("String", _String, _String?.GetType() ?? typeof(string));
    }
}

Support

To support serialization, all property types need to be a value type (i.e. primitive, enum, or struct), string, or implement the ISerializable interface. Arrays of those types are also supported.

Clone this wiki locally