简体   繁体   中英

Using protobuf-net with code auto-generated (from WSDL spec in this case)

My problem is similar to this one , except that the code I want to use with protobuf-net was generated by the WSDL utility. "They tell me" that it's not appropriate to plan to modify the generated code (to add attributes).

It would be odd to write something to build a matching set of DTO classes, given that (other than the methods described) all the classes are already public (partial) classes marked with [Serializable] and have all public properties -- that is, they are very DTO-like already.

The simplest solution I've seen is to violate the rules and modify the code. If all strings in the generated C# file

public partial class

are replaced with

[ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]
public partial class

from my limited testing that will do the trick. Does a better solution exist? Is there something wrong with using that technique (other than that I'm modifying generated code)?

Type-level attributes in partial classes can be added in separate code files. All you need is a separate file with just :

[ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]
public partial class {}

it must also be in the right namespace , but you didn't say which, so probably:

namespace Foo {
    [ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]
    public partial class {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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