C# لا إلغاء التسلسل منشئ الحصول على استدعاء بدلا من الافتراضي منشئ ؟

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