猿问

创建另一个类的后代

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


    [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”使用序列化和反序列化函数的功能。不想创建一个全新的类并复制所有代码,这些代码将在基础中。我希望能够创建一个 Class_B 对象,然后调用:


    Class_B cb = new Class_B();

...


    bformatter.Serialize(stream, cb);

...


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


手掌心
浏览 149回答 3
3回答

ITMISS

按照建议,我从 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 和 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    {    }如果那行得通,那好吧。:( 但是,那只是多余的不必要的代码,这让我很伤心。

大话西游666

我正在转换的内容:&nbsp; &nbsp; //My base class&nbsp; &nbsp; [ProtoContract]&nbsp; &nbsp; public class grid&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [ProtoMember(1), CategoryAttribute("Grid Settings"), DescriptionAttribute("Number of Horizontal GRID Segments.")]&nbsp; &nbsp; &nbsp; &nbsp; public int HorizontalCells { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; //more properties&nbsp; &nbsp; }&nbsp; &nbsp; //Our Protostub ... Originally, just for Stackoverflow.&nbsp; But, I'm liking the name.&nbsp; &nbsp; [ProtoContract]&nbsp; &nbsp; [ProtoInclude(7, typeof(SKA_Segments))]&nbsp; &nbsp; public class ProtoStub : grid&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; SKA_Segments seg_map;&nbsp; &nbsp; [ProtoContract]&nbsp; &nbsp; public class SKA_Segments : ProtoStub&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public SKA_Segments()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public override void Draw(Graphics Graf, Pen pencil)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.Draw(Graf, pencil);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //Serialization stuff&nbsp; &nbsp; seg_map.HorizontalCells = 1000;&nbsp; //test property comes from the grid class&nbsp; &nbsp; //Need to stream all three of these into the one file&nbsp; &nbsp; //Serializer.SerializeWithLengthPrefix(stream, map, PrefixStyle.Base128, 1);&nbsp; &nbsp; //Serializer.SerializeWithLengthPrefix(stream, col_map, PrefixStyle.Base128, 1);&nbsp; &nbsp; Serializer.SerializeWithLengthPrefix(stream, seg_map, PrefixStyle.Base128, 1);&nbsp; &nbsp; //De-Serialization stuff&nbsp; &nbsp; //map = Serializer.DeserializeWithLengthPrefix<SKA_MAP>(stream, PrefixStyle.Base128, 1);&nbsp; &nbsp; //col_map = Serializer.DeserializeWithLengthPrefix<SKA_Collision>(stream, PrefixStyle.Base128, 1);&nbsp; &nbsp; seg_map = Serializer.DeserializeWithLengthPrefix<SKA_Segments>(stream, PrefixStyle.Base128, 1);
随时随地看视频慕课网APP
我要回答