Skip to content

Instantly share code, notes, and snippets.

@swkwon
Created January 14, 2014 02:48
Show Gist options
  • Select an option

  • Save swkwon/8412208 to your computer and use it in GitHub Desktop.

Select an option

Save swkwon/8412208 to your computer and use it in GitHub Desktop.
protobuf
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