如何获得protobuf网扁平化和净unflatten继承类? [英] How to get protobuf-net to flatten and unflatten inherited classes in .Net?

查看:126
本文介绍了如何获得protobuf网扁平化和净unflatten继承类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个跟进的问题我前一个的如何获得protobuf网和protostuff相互支持继承了.Net和Java类?

This is a follow up question to my previous one, How to get protobuf-net and protostuff to mutually support inherited classes in .Net and Java?

作为解释在那里,我做一个Windows系统上的现有基于.NET的程序和新的方案,我创建的Andr​​oid设备之间的通信。在.NET结尾我用马克Gravell的伟大protobuf网方案,并在Android结束我使用俞卫锋的伟大protostuff程序。我用我的(大多已经存在的)C#类的定义类,并通过.proto文件我创建对应的Java类的生成。

As explained there, I'm doing communications between an existing .Net-based program on a Windows system and a new program I'm creating for Android devices. On the .Net end I'm using Marc Gravell's great protobuf-net program, and on the Android end I'm using David Yu's great protostuff program. I'm using my (mostly already-existing) C# classes as the "defining classes", and via a generated .proto file I'm creating corresponding Java classes.

事情的工作相当好,但我有很多我的C#类都继承自其他类的问题,往往是3个或4个或更多的深度。

Things are working fairly well, except that I have the problem that many of my C# classes are inherited from other classes, often to a depth of 3 or 4 or more.

在我曾建议试图Java类复制这种继承结构中的其他问题。我现在已经改变了主意,并希望更简单的东西,哪些只需要改变交易的净结束。

In the other question I had suggested trying to replicate this inheritance structure in the Java classes. I've now changed my mind, and would prefer something simpler and which would only require changes to the .Net end of the transactions.

下面是我的一些C#类(简体):

Here are some of my C# classes (simplified):

   public /* abstract */ class ProgramInfoBase
   {
      private string _programName;

      private string _programVersion;

      [ProtoMember(1)]
      public string ProgramName
      {
         get { return _programName; }
         set { _programName = value; }
      }

      [ProtoMember(2)]
      public string ProgramVersion
      {
         get { return _programVersion; }
         set { _programVersion = value; }
      }
   }


   public class ProgramInfoAndroid : ProgramInfoBase
   {
      private string _androidDeviceName;

      [ProtoMember(11)]
      public string AndroidDeviceName
      {
         get { return _androidDeviceName; }
         set { _androidDeviceName = value; }
      }
   }


   public class ProgramInfoWindows : ProgramInfoBase
   {
      private string _windowsMachineName;

      [ProtoMember(11)]
      public string WindowsMachineName
      {
         get { return _windowsMachineName; }
         set { _windowsMachineName = value; }
      }
   }


   public class ProgramInfoAndroidClient : ProgramInfoAndroid
   {
      private LogOnRequestBase _firstLogOn;

      [ProtoMember(101)]
      public LogOnRequestBase FirstLogOn
      {
         get { return _firstLogOn; }
         set { _firstLogOn = value; }
      }
   }


   public class ProgramInfoForOcc : ProgramInfoWindows
   {
      private string _licenseCustomerName = "Unlicensed demo version";

      private bool _assistanceEditionLicensed = false;

      [ProtoMember(101)]
      public string LicenseCustomerName
      {
         get { return _licenseCustomerName; }
         set { _licenseCustomerName = value; }
      }

      [ProtoMember(102)]
      public bool AssistanceEditionLicensed
      {
         get { return _assistanceEditionLicensed; }
         set { _assistanceEditionLicensed = value; }
      }
   }



从这些C#类我想一些方法自动生成以下.proto文件,其中的类继承已经扁平化:

From these C# classes I'd like some way to automatically generate the following .proto file, where the class inheritance has been "flattened":

message ProgramInfoAndroidClient {
   optional string ProgramName = 1;
   optional string ProgramVersion = 2;
   optional string AndroidDeviceName = 11;
   optional LogOnRequestBase FirstLogOn = 101;
}

message ProgramInfoForOcc {
   optional string ProgramName = 1;
   optional string ProgramVersion = 2;
   optional string WindowsMachineName = 11;
   optional string LicenseCustomerName = 101;
   optional bool AssistanceEditionLicensed = 102 [default = false];
}

和,当然,我想protobuf网自动做序列化和ProgramInfoAndroidClient和ProgramInfoForOcc类反序列化所以它的工作原理与扁平协议缓存定义和电线上的格式。

And, of course, I'd like protobuf-net to automatically do the serialization and deserialization of the ProgramInfoAndroidClient and ProgramInfoForOcc classes so it works with the flattened protocol buffer definition and the format on the wire.

推荐答案

我在做一些protobuf.net脏的变化反映你需要protostuff的那些方面,除了产生.proto文件。如果您仍然没有找到问题的解决办法,我可以分享修补protobuf.net你。

I made some dirty changes in protobuf.net that reflect those aspects of protostuff you need, except generating .proto files. If you still didn't find a solution for your problem, I can share patched protobuf.net with you.

这篇关于如何获得protobuf网扁平化和净unflatten继承类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆