标签: protobuf-net

protobuf-net 的 [ProtoInclude(1, "MyClass")] 不起作用

我正在将 protobuf-net v2 beta r431 用于 C# .net 4.0 应用程序。在我的应用程序中Dictionary<int, IMyClass>,我需要序列化一个。一个类MyClass实现IMyClass接口。根据 protobuf 的文档,我编写的代码如下:

[ProtoContract]
[ProtoInclude(1, typeof(MyClass))]
public interface IMyClass
{
    int GetId();
    string GetName();
}

[ProtoContract]
[Serializable]
public class MyClass : IMyClass
{
    [ProtoMember(1)]
    private int m_id = 0;

    [ProtoMember(2)]
    private string m_name = string.Empty;

    public MyClass(int id, string name)
    {
        m_id = id;
        m_name = name;
    }

    public MyClass()
    {
    }

    #region IMyClass Members

    public int GetId()
    {
        return m_id;
    }

    public string …
Run Code Online (Sandbox Code Playgroud)

c# protobuf-net

5
推荐指数
2
解决办法
1658
查看次数

与 protobuf-net 和 C# 的接口

有人知道为接口设置ProtoContract的正确方法是什么吗?

我得到以下异常“一旦生成序列化程序,就无法更改类型”,仅使用属性。

使用的代码:

    [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; }
    }
Run Code Online (Sandbox Code Playgroud)

只有添加以下设置,我才能反序列化:

  RuntimeTypeModel.Default.Add(typeof (ILesson5TestInteface1), true)
      .AddSubType(50, typeof(Lesson5TestClass2));
Run Code Online (Sandbox Code Playgroud)

我真的很想只使用属性来配置它。

我正在使用 NuGet 的 protobuf-net r470。

顺便说一句:这个例子来自一组“通过测试的教训”,展示了如何为我的同事使用 protobuf-net 进行序列化。

谢谢阅读 :)

.net c# protobuf-net

5
推荐指数
1
解决办法
5006
查看次数

protobuf 中的“1”、“2”、“3”是什么意思?

package tutorial;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}
Run Code Online (Sandbox Code Playgroud)

“1”、“2”、“3”是什么意思?

protocol-buffers protobuf-net

5
推荐指数
2
解决办法
1574
查看次数

ProtoBuf-Net ProtoInclude 泛型类型子类

我在使用从泛型类继承的对象的子类的 ProtoBuf-Net 时遇到了一些问题。

我的继承树看起来像这样:

Node
    SomeNodeType
    SomeOtherType
    ResourceNode<T>
        ShipResource : ResourceNode<Ship>
        SomeResource : ResourceNode<SomeType>
Run Code Online (Sandbox Code Playgroud)

我一直在所有普通类型的基本 Node 类型上使用 ProtoInclude。

使用 protobuf-net 实现这种层次结构的最佳方法是什么?我试过只包含所有内容,但我得到的错误似乎源于 protobuf 试图将对象反序列化为它的父对象之一。

protobuf-net

5
推荐指数
1
解决办法
4530
查看次数

如何序列化数组?

症状:没有为类型定义序列化程序:System.Array

由于所有C#数组都继承自Array类,我认为这在ProtoBuf-net中是有效的

[ProtoMember(1)]
public Array SomeKindOfArray = new int[]{1,2,3,4,5};

[ProtoMember(2)]
public List<Array> SomeKindOfList = new List<Array>();
Run Code Online (Sandbox Code Playgroud)

我应该使用RuntimeTypeModel注册Array吗?

m.Add(typeof (Array), true);  // does not seem to help
Run Code Online (Sandbox Code Playgroud)

尝试:

new int[]{1,2,3,4} // works

(object)new int[] { 1, 2, 3, 4 } // does not work

(Array)new int[] { 1, 2, 3, 4 } // does not work
Run Code Online (Sandbox Code Playgroud)

另一种可能的解决方案(现在找不到SO url):

有一个基类型类项的列表.

包裹每种类型.例如

class TheBase{}

class ArrayOfInt { public int[] value;}
Run Code Online (Sandbox Code Playgroud)

然后,它将成为转换到包装器列表之一.有更简单的方法吗?

c# protobuf-net

5
推荐指数
1
解决办法
4714
查看次数

有没有办法在protobuf-net代理类中定义替代转换函数(从/到接口)

使用protobuf-net v2 build 668,我正在尝试序列化/反序列化一个类,该类包含一个定义为接口的成员,同时进行即时转换.通常,代理方法可以正常工作,但由于C#不允许用户定义的接口转换,我无法定义转换.

谢谢,

namespace ProtoBufNetTest
{
    using System.Diagnostics;
    using System.IO;

    using ProtoBuf;
    using ProtoBuf.Meta;

    class Program
    {
        static void Main()
        {
            RuntimeTypeModel.Default.Add(typeof(IDummy), false)
                .SetSurrogate(typeof(DummySurrogate));

            var container = new Container { Data = new Dummy { Positive = 3 } };

            using (var file = File.Create("test.bin"))
            {
                Serializer.Serialize(file, container);
            }

            using (var file = File.OpenRead("test.bin"))
            {
                container = Serializer.Deserialize<Container>(file);
                Debug.Assert(container.Data.Positive == 3);
            }
       }
    }

    // Outside of the project, cannot be changed
    public interface IDummy
    {
        int Positive …
Run Code Online (Sandbox Code Playgroud)

c# protobuf-net

5
推荐指数
1
解决办法
328
查看次数

使用protobuf-net继承时如何选择字段编号?

我正在使用protobuf-net来序列化许多类型,其中一些类型是从基类型继承而来的.我知道Protocol Buffers规范不支持继承,并且protobuf-net中的支持基本上是一种解决方法,因为这样.

而不是使用protobuf-net属性我正在配置自定义RuntimeTypeModel,并使用AddAddSubType方法.我不太了解的我应该如何确定传递给AddSubType方法的字段编号使用哪些数字(也就是在ProtoInclude属性中使用的数字).

这个问题和其他几个问题并没有真正描述如何选择场数,事实上我已经看到了许多不同的变化:4和5; 7和8; 101&102&103; 20; 500; 显然他们的选择是为了不相互冲突,但他们如何选择的呢?什么决定从哪个数字开始?

下面的代码是一个人为的例子,但它确实与我的heirarchy(一个Event有两个派生子类型的基类型)相匹配.

using System;
using System.Collections.Generic;
using ProtoBuf.Meta;

namespace Test
{
    public sealed class History
    {
        public History()
        {
            Events = new List<Event>();
        }

        public ICollection<Event> Events { get; private set; }
    }

    public enum EventType
    {
        ConcertStarted, ConcertFinished, SongPlayed
    }

    public class Event
    {
        public EventType …
Run Code Online (Sandbox Code Playgroud)

c# inheritance protobuf-net

5
推荐指数
1
解决办法
424
查看次数

protobuf-net 从子级到父级的继承

我有一个家长班,我想要有很多扁平的孩子。这意味着一个类固有 10 个或更多不同的类。

\n\n

这是我所拥有的。

\n\n

基类:

\n\n
[ProtoContract]\n[ProtoInclude(500, typeof(Message1Send))]\n[ProtoInclude(501, typeof(Message2Send))]\npublic class MessageBase\n{\n    [ProtoMember(1)]\n    public string Topic {get;set;}\n    [ProtoMember(2)]\n    public string Action { get; set; }       \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

许多子课程中的 2 个:

\n\n
[ProtoContract]    \npublic class Message1Send : MessageBase\n{        \n    [ProtoMember(1)]\n    public string Property1 { get; set; }\n}\n\n[ProtoContract]    \npublic class Message2Send : MessageBase\n{        \n    [ProtoMember(1)]\n    public string Property1 { get; set; }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我希望能够告诉子对象我是基类的一部分。

\n\n

我不知道如何达到我的基类如下所示的地步:

\n\n
[ProtoContract]\n[ProtoInclude(500, typeof(Message1Send))]\n[ProtoInclude(501, typeof(Message2Send))]\n[ProtoInclude(502, typeof(Message3Send))]\n[ProtoInclude(503, typeof(Message4Send))]\n[ProtoInclude(504, typeof(Message5Send))]\n[ProtoInclude(505, typeof(Message6Send))]\n[ProtoInclude(506, typeof(Message7Send))]\n[ProtoInclude(507, typeof(Message8Send))]\n[ProtoInclude(508, typeof(Message9Send))]\n[ProtoInclude(509, typeof(Message10Send))]\npublic class MessageBase\n{\n    [ProtoMember(1)]\n …
Run Code Online (Sandbox Code Playgroud)

c# inheritance protocol-buffers protobuf-net

5
推荐指数
1
解决办法
779
查看次数

在 protobuf-net 中发出值类型默认值的序列化

在我的项目中,我不仅需要时间,还需要有关 UTC 偏移量的信息。因此我考虑使用DateTimeOffset类。

我没有做任何魔法,只是声明了struct包含shortfor offset 和longfor ticks (与 中相同DateTimeOffset) - 当然还定义了ProtoContractProtoMember属性。

但是:即使这个值没有初始化(两个字段都为零),线路上还有 2 个额外的字节(我用 a 进行了交叉检查class)!

我尝试继承DefaultValueAttribute并设置我的默认值 - 但也无济于事。

原因是 protobuf-net 检查按DefaultValueAttribute名称(在MetaType.ApplyDefaultBehaviour(bool isEnum, ProtoMemberAttribute normalizedAttribute))。

if ((attrib = GetAttribute(attribs, "System.ComponentModel.DefaultValueAttribute")) != null)
{
      object tmp;
      if(attrib.TryGet("Value", out tmp)) defaultValue = tmp;
}
Run Code Online (Sandbox Code Playgroud)

但即使我改变了这一点,我也会抱怨例外DefaultValueDecorator.Read(Compiler.CompilerContext ctx, Compiler.CodeLabel label)

 default:
      throw new NotSupportedException("Type cannot be represented as …
Run Code Online (Sandbox Code Playgroud)

.net c# protobuf-net

5
推荐指数
0
解决办法
357
查看次数

Keras和Spyder的奇怪错误

我正在使用Spyder与Keras一起做一些小项目,并且时不时地(我没有弄清楚使它出现的代码是什么)我得到了以下消息:

  File "~/.local/lib/python3.5/site-packages/google/protobuf/descriptor_pb2.py", line 1771, in <module>
    __module__ = 'google.protobuf.descriptor_pb2'

TypeError: A Message class can only inherit from Message
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我使用终端在Spyder之外执行程序,则不会引发此异常。我环顾四周,发现没有人在使用Keras时遇到此错误。

重新启动Spyder会使它消失,但令人沮丧。是什么原因造成的?

python protobuf-net spyder keras

5
推荐指数
0
解决办法
2012
查看次数

标签 统计

protobuf-net ×10

c# ×7

.net ×2

inheritance ×2

protocol-buffers ×2

keras ×1

python ×1

spyder ×1