C#デフォルトのコンストラクタの代わりにDESERIALIZALIZATIONコンストラクタが呼び出されますか?

StackOverflow https://stackoverflow.com//questions/9662734

質問

私はC#のオブジェクトの直列化に精通しているだけです。デフォルトコンストラクターの代わりに、またはそれに加えて、デシリアライズコンストラクタが呼び出されているかどうか疑問に思います。それに加えて、これらはどの順序で呼ばれていますか?例えば:

[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));
    }
}
.

役に立ちましたか?

解決

いいえデフォルトの「代わりに」と呼ばれます - しかし、あなたはこのようなものでリストを初期化することができます:

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));
}
.

" ...:この()" - 構文 - しかしあなたの特別なケースでは必要ありません!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top