创建另一个类的后代

时间:2018-05-09 22:28:46

标签: c# class inheritance serialization

我想根据以下内容创建一个新类:

    [Serializable()]
    public class Class_A : ISerializable
    {
        public int DISP_SEGS { get; set; }

        //Default constructor
        public Class_A()
        {
            //
        }

        //Deserialization constructor
    //If I add virtual to this I get an error
        public Class_A(SerializationInfo info, StreamingContext ctxt)
        {
            foreach (PropertyInfo PI in this.GetType().GetProperties()) PI.SetValue(this, info.GetValue(PI.Name, PI.PropertyType));
        }

        //Serialization function
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            foreach (PropertyInfo PI in this.GetType().GetProperties()) info.AddValue(PI.Name, PI.GetValue(this));
        }
    }

我希望“Class_B”使用Serialization& amp;的功能。去序列化功能。不想创建一个全新的类并复制所有代码,这些代码将位于基础中。我希望能够只创建一个Class_B对象,然后调用:

    Class_B cb = new Class_B();

...

    bformatter.Serialize(stream, cb);

...

    cb = (Class_B)bformatter.Deserialize(stream);

2 个答案:

答案 0 :(得分:1)

根据建议,我从Binaryformatter切换到Protobuf。之后,有几个问题,我已经开始工作了。所以,我以新格式回到原来的问题。

我有一个基类,想要创建一些不同的类。我看到了,你想要使用Protobufs ProtoInclude属性。但是,它似乎落后了。它说,我的基类必须知道派生类型。

    [ProtoContract]
    [ProtoInclude(7, typeof(SomeDerivedType))]
    class SomeBaseType {...}

    [ProtoContract]
    class SomeDerivedType {...}

这是一个独立的课程,我来自。所以,它需要是通用的。

    [ProtoContract]
    public class grid
    {
    }

所以,我需要从中获得。

    [ProtoContract]
    public class Class_A : grid
    {
    }  

    [ProtoContract]
    public class Class_B : grid
    {
    }

将ProtoInclude放在Class_A&我明白,Class_B没有做任何事情。不,如果它需要在网格类上。最好的方法是什么?因为,我正在打字,我想知道是否需要创建一个知道Class_A& amp;的存根类。 Class_B?

    [ProtoContract]
    [ProtoInclude(7, typeof(Class_A))]
    [ProtoInclude(8, typeof(Class_B))]
    public class ProtoStub : grid
    {
    }

    [ProtoContract]
    public class Class_A : ProtoStub
    {
    }

    [ProtoContract]
    public class Class_B : ProtoStub
    {
    }

如果有效,那好吧。 :(但是,这只是额外的不必要的代码,这让我感到难过。

答案 1 :(得分:0)

我要转换的内容:

    //My base class
    [ProtoContract]
    public class grid
    {
        [ProtoMember(1), CategoryAttribute("Grid Settings"), DescriptionAttribute("Number of Horizontal GRID Segments.")]
        public int HorizontalCells { get; set; }

        //more properties
    }

    //Our Protostub ... Originally, just for Stackoverflow.  But, I'm liking the name.
    [ProtoContract]
    [ProtoInclude(7, typeof(SKA_Segments))]
    public class ProtoStub : grid
    {
    }

    SKA_Segments seg_map;

    [ProtoContract]
    public class SKA_Segments : ProtoStub
    {
        public SKA_Segments()
        {
        }

        public override void Draw(Graphics Graf, Pen pencil)
        {
            base.Draw(Graf, pencil);
        }
    }

    //Serialization stuff
    seg_map.HorizontalCells = 1000;  //test property comes from the grid class

    //Need to stream all three of these into the one file
    //Serializer.SerializeWithLengthPrefix(stream, map, PrefixStyle.Base128, 1);
    //Serializer.SerializeWithLengthPrefix(stream, col_map, PrefixStyle.Base128, 1);
    Serializer.SerializeWithLengthPrefix(stream, seg_map, PrefixStyle.Base128, 1);

    //De-Serialization stuff
    //map = Serializer.DeserializeWithLengthPrefix<SKA_MAP>(stream, PrefixStyle.Base128, 1);
    //col_map = Serializer.DeserializeWithLengthPrefix<SKA_Collision>(stream, PrefixStyle.Base128, 1);
    seg_map = Serializer.DeserializeWithLengthPrefix<SKA_Segments>(stream, PrefixStyle.Base128, 1);