Question

Je suis en train de devenir familier avec la sérialisation des objets en C #.Je me demande si le constructeur de désérialisation est appelé au lieu du constructeur par défaut ou en plus.Si c'est en plus de, quel ordre sont-ils appelés?Par exemple:

[Serializable()]
public class ReadCache : ISerializable
{
    protected ArrayList notifiedURLs;

    // Default constructor
    public ReadCache()
    {
        notifiedURLs = new ArrayList();
    }

    // Deserialization constructor.
    public ReadCache(SerializationInfo info, StreamingContext ctxt)
    {
        //Get the values from info and assign them to the appropriate properties
        notifiedURLs = (ArrayList)info.GetValue("notifiedURLs", typeof(ArrayList));
    }
}

Était-ce utile?

La solution

Non, il sera appelé "à la place" de la valeur par défaut - mais vous pouvez initialiser votre liste avec quelque chose comme ceci:

public ReadCache(SerializationInfo info, StreamingContext ctxt)
  : this()
{
    //Get the values from info and assign them to the appropriate properties
    notifiedURLs = (ArrayList)info.GetValue("notifiedURLs", typeof(ArrayList));
}

Veuillez noter le " ...: ceci () " - Syntaxe - mais dans votre cas particulier, vous n'êtes pas obligé!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top