简体   繁体   English

如何从用属性修饰的 C# 类生成 .proto 文件?

[英]How do I generate a .proto file from a C# class decorated with attributes?

Trying to get my mind around google protobuf.试图让我的想法围绕 google protobuf。 I found some implementation of protobuf in C# but they seems to lack one feature: the ability to generate .proto files automatically from an existing C# class decorated with attributes.我在 C# 中找到了 protobuf 的一些实现,但它们似乎缺少一个功能:能够从现有的 C# 类中自动生成 .proto 文件,这些类用属性修饰。

The reason I want to do it this way instead of going from auto-generated C# classes from .proto file is because I already have the C# classes defined in my project and I don't want to duplicate them just to satisfy ProtoBuf.我想这样做而不是从 .proto 文件中自动生成的 C# 类的原因是因为我已经在我的项目中定义了 C# 类,我不想复制它们只是为了满足 ProtoBuf。

Does anyone have encountered such a scenario?有没有人遇到过这样的场景?


Update更新

Is this possible to just decorate a C# class and not use a .proto file to use protobuf?是否可以只装饰 C# 类而不使用 .proto 文件来使用 protobuf?

Good news;好消息; what you have described (having existing C# classes) is the expected use-case of protobuf-net.您所描述的(具有现有的 C# 类)是 protobuf-net 的预期用例。 All the .proto stuff ("protogen", the VS add-in, etc) were all added as afterthoughts .所有 .proto 的东西(“protogen”、VS 插件等)都是作为事后的想法添加的 The core of protobuf-net doesn't know about them or care about them. protobuf-net 的核心不知道或不关心它们。

protocol buffers defines a DSL (.proto, as you mention) that is shared between implementations, and is (sometimes) used for code generation. protocol buffers 定义了一个在实现之间共享的 DSL(.proto,正如你所提到的),并且(有时)用于代码生成。 When I first wrote protobuf-net, the code-generation aspect wasn't my biggest concern - simply that .NET developers are generally guilty (myself included) of "implementation first" rather than "contract first".当我第一次编写 protobuf-net 时,代码生成方面并不是我最关心的问题——简单地说,.NET 开发人员通常对“实现优先”而不是“契约优先”感到内疚(包括我自己)。

As a consequence, protobuf-net doesn't need .proto files to work;因此,protobuf-net不需要 .proto 文件即可工作; an attributed class is sufficient to unambiguously serialize/deserialize.属性类足以明确地序列化/反序列化。 Just use Serializer.Serialize , .Merge and .Deserialize (etc).只需使用Serializer.Serialize.Merge.Deserialize (等)。

That said;那说; it does include some very under-developed and experimental support for this:确实包括一些非常不成熟和实验性的支持:

string proto = Serializer.GetProto<YourType>();

This is far from complete, but may work for simple types.这还远未完成,但可能适用于简单类型。 If you have some specific cases where it fails, then let me know (add a comment or log an issue).如果您有一些失败的特定情况,请告诉我(添加评论或记录问题)。 However;然而; most of the time, people interested in .proto would write the .proto first and work from there.大部分时间,有兴趣的.proto人会写.proto第一和工作从那里。

Examples of working decorated types are shown on the project home page ;工作装饰类型的示例显示在项目主页上 it is entirely up to you whether you use WCF attributes, xml attributes or protobuf-net attributes (although the latter provide more control over some specific serialization points, such as inheritance and numeric layouts).是否使用 WCF 属性、xml 属性或 protobuf-net 属性完全取决于您(尽管后者提供了对某些特定序列化点的更多控制,例如继承和数字布局)。

Skeet Marc 跑到这里并获得巨大成功之前,让我指出protobuf.net。

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

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