开源软件名称(OpenSource Name):protobuf-net/protobuf-net开源软件地址(OpenSource Url):https://github.com/protobuf-net/protobuf-net开源编程语言(OpenSource Language):C# 94.8%开源软件介绍(OpenSource Introduction):protobuf-netprotobuf-net is a contract based serializer for .NET code, that happens to write data in the "protocol buffers" serialization format engineered by Google. The API, however, is very different to Google's, and follows typical .NET patterns (it is broadly comparable, in usage, to Release NotesChange history and pending changes are here. Supported Runtimes
Build toolsBuild tools to help you use protobuf-net correctly are available via Runtime InstallationAll stable and some pre-release packages are available on NuGet. CI Builds are available via MyGet (feed URL: You can use the following command in the Package Manager Console: Install-Package protobuf-net
Basic usage1 First Decorate your classes[ProtoContract]
class Person {
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public string Name {get;set;}
[ProtoMember(3)]
public Address Address {get;set;}
}
[ProtoContract]
class Address {
[ProtoMember(1)]
public string Line1 {get;set;}
[ProtoMember(2)]
public string Line2 {get;set;}
} Note that unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member. Additionally, to show intent it is necessary to show that we intend this type to be serialized (i.e. that it is a data contract). 2 Serialize your dataThis writes a 32 byte file to "person.bin" : var person = new Person {
Id = 12345, Name = "Fred",
Address = new Address {
Line1 = "Flat 1",
Line2 = "The Meadows"
}
};
using (var file = File.Create("person.bin")) {
Serializer.Serialize(file, person);
} 3 Deserialize your dataThis reads the data back from "person.bin" : Person newPerson;
using (var file = File.OpenRead("person.bin")) {
newPerson = Serializer.Deserialize<Person>(file);
} NotesNotes for Identifiers
Advanced subjectsInheritanceInheritance must be explicitly declared, in a similar way that it must for XmlSerializer and DataContractSerializer. This is done via [ProtoInclude(...)] on each type with known sub-types: [ProtoContract]
[ProtoInclude(7, typeof(SomeDerivedType))]
class SomeBaseType {...}
[ProtoContract]
class SomeDerivedType {...} There is no special significance in the 7 above; it is an integer key, just like every [ProtoMember(...)]. It must be unique in terms of SomeBaseType (no other [ProtoInclude(...)] or [ProtoMember(...)] in SomeBaseType can use 7), but does not need to be unique globally. .proto fileAs an alternative to writing your classes and decorating them, You can generate your types from a .proto schema using Alternative to attributesIn v2+, everything that can be done with attributes can also be configured at runtime via SupportI try to be responsive to Stack Overflow questions in the |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论