简体   繁体   English

如何使protobuf-net展平和展平.Net中的继承类?

[英]How to get protobuf-net to flatten and unflatten inherited classes in .Net?

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? 这是我上一个问题的后续问题,即如何使protobuf-net和protostuff相互支持.Net和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. 如此处所述,我正在Windows系统上现有的基于.Net的程序与我为Android设备创建的新程序之间进行通信。 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. 在.Net端,我正在使用Marc Gravell的出色protobuf-net程序,在Android端,我正在使用David Yu的出色的protostuff程序。 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#类用作“定义类”,并通过生成的.proto文件创建相应的Java类。

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. 事情运行得很好,除了我的问题是我的许多C#类都是从其他类继承的,通常深度是3或4或更多。

In the other question I had suggested trying to replicate this inheritance structure in the Java classes. 在另一个问题中,我建议尝试在Java类中复制此继承结构。 I've now changed my mind, and would prefer something simpler and which would only require changes to the .Net end of the transactions. 现在,我改变了主意,希望使用更简单的方法,并且只需要更改事务的.Net端即可。

Here are some of my C# classes (simplified): 这是我的一些C#类(简化):

   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; }
      }
   }

From these C# classes I'd like some way to automatically generate the following .proto file, where the class inheritance has been "flattened": 从这些C#类中,我想要一种自动生成以下.proto文件的方法,该类继承已被“扁平化”:

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];
}

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自动执行ProgramInfoAndroidClient和ProgramInfoForOcc类的序列化和反序列化,以便它与扁平化的协议缓冲区定义和网络上的格式一起使用。

I made some dirty changes in protobuf.net that reflect those aspects of protostuff you need, except generating .proto files. 我在protobuf.net中做了一些肮脏的更改,这些更改反映了您需要的原型的那些方面,除了生成.proto文件之外。 If you still didn't find a solution for your problem, I can share patched protobuf.net with you. 如果您仍然没有找到解决问题的方法,我可以与您共享修补后的protobuf.net。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM