3 回答
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
{
}
如果那行得通,那好吧。:( 但是,那只是多余的不必要的代码,这让我很伤心。
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);
- 3 回答
- 0 关注
- 148 浏览
添加回答
举报