Created
January 14, 2014 02:48
-
-
Save swkwon/8412208 to your computer and use it in GitHub Desktop.
protobuf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using ProtoBuf; | |
| namespace ConsoleApplication43 | |
| { | |
| [ProtoContract] | |
| class Packet | |
| { | |
| [ProtoMember(1)] | |
| public int Index { get; set; } | |
| [ProtoMember(2)] | |
| public string Name { get; set; } | |
| [ProtoMember(3)] | |
| public Dictionary<int, Item> InventoryList { get; set; } | |
| } | |
| [ProtoContract] | |
| class Item | |
| { | |
| [ProtoMember(1)] | |
| public int Index { get; set; } | |
| [ProtoMember(2)] | |
| public int ItemId { get; set; } | |
| [ProtoMember(3)] | |
| public int ItemLevel { get; set; } | |
| } | |
| [ProtoContract] | |
| class Test | |
| { | |
| [ProtoMember(1)] | |
| public string TestName { get; set; } | |
| [ProtoMember(1, Options=MemberSerializationOptions.Required)] | |
| public string TestNickName { get; set; } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| ProtoFileWrite<Packet>(); | |
| ProtoFileWrite<Test>(); | |
| } | |
| static void ProtoFileWrite<T>() where T : class | |
| { | |
| string subPath = "./proto"; | |
| System.IO.DirectoryInfo dInfo = new DirectoryInfo(subPath); | |
| if (!dInfo.Exists) | |
| System.IO.Directory.CreateDirectory(subPath); | |
| string protoFileName = string.Format("{0}.proto", typeof(T).Name); | |
| string protoFilePath = Path.Combine(subPath, protoFileName); | |
| string objectProtoFileData = Serializer.GetProto<T>(); | |
| using (StreamWriter sw = new StreamWriter(protoFilePath)) | |
| { | |
| sw.WriteLine(objectProtoFileData); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment