防止基类的序列化

Won*_*ane 5 c# serialization

我觉得我应该知道这一点,但出于某种原因....

序列化从(可能是抽象的)基类派生的类的首选方法是什么,而不必一直序列化备份树?例如,您可能无法控制从中派生的类,但希望使用序列化来克隆您的对象(仅限您的对象,而不是基础).

例如:

// This is a base class that is outside my control, which derives from
// some other base class that I know "nothing" about
public abstract class SomeBaseClass : SomeOtherBaseClass
{
    private string mBaseProperty = "Base Property";
    public string BaseProperty
    {
        get { return mBaseProperty; }
        set { mBaseProperty = value; }
    }
}

// This is the class that I do control
[Serializable()]
private class MyDerivedClass : SomeBassClass
{
    // Assume normal constructors, etc.

    // Here are some properties
    private string mDerivedPropertyOne = String.Empty;
    private string DerivedPropertyOne
    {
        get { return mDerivedPropertyOne ; }
        set { mDerivedPropertyOne = value; }
    }

    private string mDerivedPropertyTwo = String.Empty;
    private string DerivedPropertyTwo
    {
        get { return mDerivedPropertyTwo ; }
        set { mDerivedPropertyTwo = value; }
    }

    // And now a quick-n-dirty Equals override
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            MyDerivedClass compareTo = obj as MyDerivedClass;
            if (compareTo == null)
                return false;

            return ((String.Compare(this.DerivedPropertyOne, 
                                    compareTo.DerivedPropertyOne, true) == 0) &&
                    (String.Compare(this.DerivedPropertyTwo, 
                                    compareTo.DerivedPropertyTwo, true) == 0) &&
        }
}

// And while we're at it, here's a simple clone found elsewhere on StackOverflow
public static class ObjectClone
{
    public static T Clone<T>(this T source)
    {
        if (!typeof(T).IsSerializable)
        {             
            throw new ArgumentException("The type must be serializable.", "source");         
        }          

        // Don't serialize a null object, simply return the default for that object         
        if (Object.ReferenceEquals(source, null))
        {             
            return default(T);         
        }          

        IFormatter formatter = new BinaryFormatter();         
        Stream stream = new MemoryStream();         
        using (stream)         
        {             
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);             
            return (T)formatter.Deserialize(stream);         
        }         
    }
}
Run Code Online (Sandbox Code Playgroud)

如上所述,这将抛出SerializationException,因为SomeBaseClass未标记为可序列化.

小智 9

简答:使用组合而不是继承.将要序列化的成员解压缩到另一个类中,并使其可序列化.这将为您提供生命周期内所需的控件以及序列化的范围.

一般来说,对于序列化对象来说,它是一个很好的模式,可以作为哑数据持有者,并通过包装它们来添加任何额外的逻辑.现代序列化框架如protobuf,thrift,avro强化了这一点,无论如何都将为这些序列化对象生成代码,并期望你不要通过继承来破坏这些类的内部.

  • 这听起来很愚蠢。我有一个继承自 A 的类 B(根据我使用的框架的设计)。我需要序列化 ​​B,但不需要序列化 ​​A。无法从“BSerializedFields”继承 B,只留下逻辑,因为这需要多重继承。我可以将“BSerializedFields”聚合为 B 中的字段“Data”,但这需要将其字段作为“obj.Data.FieldName”访问。我可以在 B 上创建一些修改器来访问“Data”,但这需要大量代码。我可以生成该代码,是的。但是,嘿,实现我自己的序列化器已经更容易了。 (2认同)
  • 所以这只是 C# 中一些写得不好的序列化例程,而不是一个好的模式。 (2认同)