亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建另一個類的后代

創建另一個類的后代

C#
手掌心 2021-06-02 13:37:04
我想根據以下內容創建一個新類:    [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);
查看完整描述

3 回答

?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

按照建議,我從 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

    {

    }

如果那行得通,那好吧。:( 但是,那只是多余的不必要的代碼,這讓我很傷心。


查看完整回答
反對 回復 2021-06-05
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

我正在轉換的內容:


    //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);



查看完整回答
反對 回復 2021-06-05
  • 3 回答
  • 0 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號