Tag: protobuf net

没有为类型定义的序列化程序:System.Windows.Media.Media3D.Point3D

我正在尝试使用protobuf net序列化一些数据。 在序列化期间,我收到一个错误,没有为Point3D类型定义序列化。 我发现了一个像这样的问题,但仍然无法实现和解决它。 链接如下: – 没有为类型定义的序列化程序:System.Drawing.Color [ProtoContract] public class ReturnPanelData { [ProtoMember(1)] public Point3D PlacedPoint3D { get; set; } [ProtoMember(2)] public double PlacementAngle { get; set; } [ProtoMember(3)] public string PanelName { get; set; } } [ProtoContract] public class ReturnDataType { [ProtoMember(1)] public List ReturnList { get; set; } [ProtoMember(2)] public double RemainderArea { […]

与protobuf-net和C#的接口

有谁知道为接口设置ProtoContract的正确方法是什么? 我得到以下exception“仅使用属性生成序列化程序后才能更改类型 ”。 使用的代码: [ProtoContract] public class Lesson5TestClass2 : ILesson5TestInteface1 { [ProtoMember(1)] public string Name { get; set; } [ProtoMember(2)] public string Phone { get; set; } } [ProtoContract] [ProtoInclude(1000, typeof(Lesson5TestClass2))] public interface ILesson5TestInteface1 { [ProtoMember(1)] string Name { get; set; } [ProtoMember(2)] string Phone { get; set; } } 只有添加以下设置,我才能反序列化: RuntimeTypeModel.Default.Add(typeof (ILesson5TestInteface1), true) .AddSubType(50, […]

如何序列化类实现了两个具有相同基础接口的接口

我正在尝试使用protobuf v2序列化一个类A实现接口B和C.并且B和C都来自D. [ProtoBuf.ProtoContract] [ProtoBuf.Includes(101, typeof(B)) [ProtoBuf.Includes(101, typeof(C)) interface D{…} [ProtoBuf.ProtoContract] [ProtoBuf.Includes(101, typeof(A)) interface B:interfae D{…} [ProtoBuf.ProtoContract] [ProtoBuf.Includes(101, typeof(A)) interface C:interface D { … } [ProtoBuf.ProtoContract] class A: interface B, interface C{…} 将返回错误:类型只能参与一个inheritance层次结构。

Protobuf-net序列化枚举值超出范围

C#允许为枚举分配任何整数值。 当我尝试使用enum字段序列化(通过protobuf-net)对象时,该值超出范围,它会抛出exception:没有将wire-value映射到枚举PersonLevel。 我的枚举PersonLevel没有Flags属性。 [ProtoContract(ImplicitFields = ImplicitFields.AllFields)] public enum PersonLevel { Unknown = 1 } [ProtoContract(ImplicitFields = ImplicitFields.AllFields)] public class Person { … public PersonLevel PersonLevel { get; set; } … } var ms = new MemoryStream(); var person = new Person { … PersonLevel = (PersonLevel) 500 … }; Serializer.Serialize(ms, person); //No wire-value is mapped […]

使用protobuf-net获得ProtoGen

我正在尝试使用protobufs序列化一些类。 我使用NuGet安装了protobuf-net,使用属性正确装饰了我的代码,一切运行都很顺利。 但是,最终消息将在C#中创建,但在其他环境中消耗,因此我需要从C#创建.proto文件,或者 – 对我来说更合理 – 从.proto文件创建C#代码。 无论我做什么,我都找不到安装在我电脑任何​​地方的ProtoGen.exe。 我也找到了这个旧post ,但是当我创建.proto文本文件时,没有发生任何类似的事情。 手动添加ProtoBufTool也不起作用。 我错过了什么?

protobuf-net是否支持可空类型?

是否有可能在protobuf-net中生成可空的成员? message ProtoBuf1 { optional Int32? databit = 1; optional Nullable databool = 2; }

使用带有标志枚举的ProtoBuf-Net时出错

在使用ProtoBuf-Net并序列化枚举属性时,枚举设置为[FlagsAttribute],在序列化由多个标志组成的枚举值时收到以下错误消息。 错误是:值(MyEnum.MyValue)没有属性MyProperty的线表示 MyEnum在哪里: [Flags] public Enum MyEnum { MyValue = 0, MyValue1 = 1, MyValue2 = 2, MyValue4 = 4, MyValue8 = 8, } 和 MyProperty = MyEnum.MyValue2 | MyEnum.MyValue4; 似乎是protobuf-net中的一个bug?

使用Protobuf-net序列化分块字节数组的内存使用情况

在我们的应用程序中,我们有一些数据结构,其中包含一个分块的字节列表(当前公开为List )。 我们将字节大块化,因为如果我们允许将字节数组放在大对象堆上,那么随着时间的推移,我们会遇到内存碎片。 我们还开始使用Protobuf-net来序列化这些结构,使用我们自己生成的序列化DLL。 但是我们注意到Protobuf-net在序列化时会创建非常大的内存缓冲区。 浏览源代码时,似乎它可能无法刷新其内部缓冲区,直到整个List结构被写入,因为它需要在缓冲区前面写入总长度。 不幸的是,这首先解决了我们的工作,首先将字节分块,最终由于内存碎片而给我们OutOfMemoryExceptions(exception发生在Protobuf-net尝试将缓冲区扩展到84k以上时,这显然是在LOH,我们的整体进程内存使用率相当低。 如果我对Protobuf-net工作原理的分析是正确的,那么有没有解决这个问题的方法呢? 更新 根据Marc的回答,这是我尝试过的: [ProtoContract] [ProtoInclude(1, typeof(A), DataFormat = DataFormat.Group)] public class ABase { } [ProtoContract] public class A : ABase { [ProtoMember(1, DataFormat = DataFormat.Group)] public BB { get; set; } } [ProtoContract] public class B { [ProtoMember(1, DataFormat = DataFormat.Group)] public List Data { get; set; } […]

protobuf-net无法反序列化我的课程

我想序列化一个’Player’类,并通过我的网络流发送给客户端。 球员类 [ProtoMember(1)] public int flag; [ProtoMember(2)] public Int16 id; [ProtoMember(3)] public MyVector3 CharPos; [ProtoMember(7)] public bool spawned; MyVector3 (由于protobuf不支持Vector3序列化这一事实) [ProtoContract] public class MyVector3 { [ProtoMember(4)] public float X { get; set; } [ProtoMember(5)] public float Y { get; set; } [ProtoMember(6)] public float Z { get; set; } public MyVector3() { this.X = 0.0f; […]

protobuf-net如何处理只读字段?

我使用protobuf-net来序列化/反序列化我的数据。 我有一些相当简单的类,所以这不是真正的问题。 据我所知,protobuf-net使用IL生成来创建序列化/反序列化代码。 虽然我的模型中只有字段,但我想知道如何用IL写入这样的字段? 我可以清楚地看到它运作良好,但我不知道为什么…… 我试图在代码中窥探它,但它有点太复杂了。 我自己尝试生成此类代码总是会导致ILvalidation程序错误。