基本クラスとそこから派生した別のクラスがあります。基本クラスには 20 個のメンバーがあり、派生クラスには 5 個のメンバーがあるとします。派生クラスのみがシリアライズ可能です。
インスタンス化すると、派生クラスのオブジェクトには 25 個のメンバーがすべて含まれます。派生クラスの5つのメンバーのみをシリアル化するにはどうすればよいですか? 「this」を使用してシリアル化または逆シリアル化すると、クラス全体 (25 メンバーすべて) がシリアル化されてから逆シリアル化されます。
コード スニペットを次に示します (完全ではありません)。
// Base class definition.
public abstract class baseMyClass
{
// declaration of members
}
...
// Derived class definition.
[Serializable]
public sealed class MyDerivedClass : baseMyClass
{
// declaration of members
}
...
// Serializing the object.
StringWriter writer = new StringWriter();
XmlSerializer xs = new XmlSerializer(typeof(MyDerivedClass));
xs.Serialize(writer, this);
...
// Deserializing the object.
StringReader reader = new StringReader(System.Text.Encoding.UTF8.GetString(data));
XmlSerializer xs = new XmlSerializer(typeof(MyDerivedClass));
MyDerivedClass objMyDerivedClass = (MyDerivedClass)(xs.Deserialize(reader));
同様の例は見つかりませんでした。ご存知の方がいらっしゃいましたら、ご指摘ください。
すべての助けをありがとう。