我如何生成饰有属性的C#类.proto文件? [英] How do I generate a .proto file from a C# class decorated with attributes?

查看:283
本文介绍了我如何生成饰有属性的C#类.proto文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图绕过谷歌protobuf的脑海里。我发现在C#中一些实现protobuf的,但他们似乎缺少一个特征:从装饰的属性现有的C#类自动生成.proto文件的能力。

我想做的事情,而不是从自动生成的C#类会从.proto文件中这样说的原因是因为我已经在我的项目中定义的C#类,我不希望重复他们只是为了满足protobuf的

有谁曾经遇到过这样的情况?


更新

这是可能的,只是装点一个C#类,而不是使用一个.proto文件中使用protobuf的?

解决方案

好消息;你所描述的(其现有的C#类)有什么预期的使用情况protobuf网的。所有.proto的东西(硫辛酸,在VS加载项等)都被添加为后记。 protobuf网的核心不知道他们或关心他们。

协议缓冲区定义DSL(.proto,你提到),它的实现之间共享,并且是(有时)用于code一代。当我第一次写protobuf网,在code代方面是不是我最关心的 - 简单地说.NET开发者的普遍有罪(包括我自己)实施第一,而不是合同第一。

因此​​,protobuf网不需要.proto文件工作;一个属性类是足以明确序列化/反序列化。只需使用 Serializer.Serialize .Merge .Deserialize (等)。

这表示;它的确实的包括一些非常欠发达和实验作为这种支持:

 字符串原= Serializer.GetProto< YourType>();
 

这是远远没有完成,但可能工作的简单类型。如果你有它失败的一些具体情况,然后让我知道(添加评论或登录问题)。然而;大部分时间,感兴趣的.proto人会写.proto的第一的工作从那里。

工作装饰类型的

例子显示在项目主页;这完全取决于你是否使用WCF属性,XML属性或protobuf网属性(尽管后者提供更多的控制某些特定的序列化点,比如继承和数字布局)。

Trying to get my mind around 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.

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.

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?

解决方案

Good news; what you have described (having existing C# classes) is the expected use-case of protobuf-net. All the .proto stuff ("protogen", the VS add-in, etc) were all added as afterthoughts. The core of protobuf-net doesn't know about them or care about them.

protocol buffers defines a DSL (.proto, as you mention) that is shared between implementations, and is (sometimes) used for code generation. 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".

As a consequence, protobuf-net doesn't need .proto files to work; an attributed class is sufficient to unambiguously serialize/deserialize. Just use Serializer.Serialize , .Merge and .Deserialize (etc).

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.

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).

这篇关于我如何生成饰有属性的C#类.proto文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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