简体   繁体   English

XSD生成器到C#/ protobuf

[英]xsd generator to C#/protobuf

i'm working on an app that is serializing and deserializing messages across the wire. 我正在开发一个应用,该应用通过网络对消息进行序列化和反序列化。 But i'm having an issue with a C# class generated from an xsd schema using xsd. 但是我在使用xsd从xsd模式生成的C#类遇到问题。

I was able to successfully test the protobuf library with my own test class. 我能够使用自己的测试类成功测试protobuf库。 installing the lib and decorating my class with the necessary protobuf attributes including integer order. 安装lib并用必需的protobuf属性(包括整数顺序)装饰我的类。

i understand from the documentation that protobuf honors existing serialization attributes like xmltype, datacontract etc. When i run the xsdgen tool my classes are decorated with those attributes but the serialization process is not happening. 我从文档中了解到protobuf尊重xmltype,datacontract等现有的序列化属性。当我运行xsdgen工具时,我的类已使用这些属性进行修饰,但是序列化过程并未发生。

i tried creating a partial class but it was still quite manual if i have many classes and the classes are constantly changing. 我尝试创建局部类,但是如果我有很多类并且这些类在不断变化,那还是相当手动的。

this is my xsd command [xsd TopClass.xsd /c /eld /edb /n:MyNamespace /order] 这是我的xsd命令[xsd TopClass.xsd / c / eld / edb / n:MyNamespace / order]

Can someone recommend a solution? 有人可以推荐解决方案吗?

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("safHeartBeat", Namespace="", IsNullable=false)]
public partial class SaFHeartBeat {

    private System.DateTime timestampField;
    private string cacheNameField;
    private string hostnameField;
    private System.DateTime processStartTimeField;
    private SafStatusEnum statusField;
    private object datatypeField;
    private int itemCountField;
    private System.DateTime lastUpdateTimeField;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public System.DateTime timestamp {
        get {
            return this.timestampField;
        }
        set {
            this.timestampField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public string cacheName {
        get {
            return this.cacheNameField;
        }
        set {
            this.cacheNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=2)]
    public string hostname {
        get {
            return this.hostnameField;
        }
        set {
            this.hostnameField = value;
        }
    }

protobuf-net can try to use attributes from other libraries, but only when they supply enough information. protobuf-net可以尝试使用其他库中的属性,但前提是它们必须提供足够的信息。 In particular, protocol buffers requires a positive integer identifier per member (which it can try to get from Order ), but: [XmlAttribute] has no Order , and xsd annoyingly starts the Order from 0 , which can't be used by protocol buffers (it is not a valid field identifier). 特别地,协议缓冲区需要每个部件(它可以尝试从得到的正整数标识符Order ),但是: [XmlAttribute] 没有 Order ,和xsd烦人启动Order0 ,这是不能由协议缓冲区被用来(它不是有效的字段标识符)。

Ultimately, this may not be a good fit for in-progress/changing definitions from xsd; 最终,这可能不适用于xsd中正在进行的/更改中的定义。 I would be tempted to say "write a separate DTO later for protobuf". 我很想说“稍后再为protobuf编写一个单独的DTO”。 However, another option is to generate a second partial class file, and decorate that , for example: 但是,另一种选择是生成第二个partial类文件,并装饰 ,例如:

namespace MyNamespace {
    [ProtoContract]
    [ProtoPartialMember(1, "Id"), ProtoPartialMember(2, "Name")]
    partial class SomeType {}

    [ProtoContract]
    [ProtoPartialMember(1, "Id"), ProtoPartialMember(2, "Date")]
    [ProtoPartialMember(3, "Value"), ProtoPartialMember(4, "Origin")]
    partial class SomeOtherType {}
}

This will still need to be maintained to include the members you need to serialize, though. 不过,仍然需要维护它以包括需要序列化的成员。

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

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