views:

283

answers:

1

Can someone tell me why this class requires [XmlInclude(typeof(AutoHedgerBaseDataObject))] to deserialize properly? It's not clear to me.

[Serializable]
[XmlInclude(typeof(AutoHedgerBaseDataObject))]
public abstract class AutoHedgerCommandMessage 
{
    #region Variables

    private string myUpdatedBy;
    private string myUpdatedTime;

    #endregion

    #region Constructors

    public AutoHedgerCommandMessage(string name)           
    {
        myUpdatedBy = Environment.UserName;
        myUpdatedTime = DateTime.Now.ToString("YYYYMMdd HH:mm:ss zzz");
    }

    #endregion

    #region Properties

    [XmlElement("updated_by")]
    public string UpdatedBy
    {
        get { return myUpdatedBy; }
        set { myUpdatedBy = value; }
    }


    [XmlElement("updated_time")]
    public string UpdatedTime
    {
        get { return myUpdatedTime; }
        set { myUpdatedTime = value; }
    }


    #endregion

    #region Methods

    protected T[] ToArrayOfType<T>(IList<string> ids, string source)
        where T : AutoHedgerBaseDataObject, new()
    {
        T[] list = new T[] { };
        if (ids != null)
        {
            list = new T[ids.Count];
            for (int i = 0; i < ids.Count; i++)
            {
                list[i] = new T();
                list[i].Id = ids[i];
                list[i].Source = source;
            }

        }
        return list;
    }       

    #endregion
}

Although we have several classes that inherit from this base class, only one of them fails to serialize without the XmlIncude and that is a class that has no serializable properties or data and does not call any methods on the base class. This is the class that fails to deserialize:

[Serializable()]
[XmlRoot(ElementName = "command")]
public class GetAutoHedgerHedgesCommand : AutoHedgerCommandMessage
{
    #region Constructors

    // Parameterless constructor for serialization/deserialization
    public GetAutoHedgerHedgesCommand()
        : base(Name)
    {
    }

    #endregion

    #region Constants

    public const string Name = "get_autohedger_hedges";

    #endregion        
}
A: 

Presumably because something in the base class or the derived class is causing a list of items to be serialised that you didnt explicitly mention in your interface (this class itself doesnt expose anything that would direcly lead to the issue).

This has been covered pretty completely in a previous question

Ruben Bartelink