zoukankan      html  css  js  c++  java
  • protobuf-net

    protobuf-net学习笔记

    最近一直在做windows phone 7手机上面的应用程序,学到了一些除了微软这套东西以外的知识,除了Funambol这个库以外(等到完全研究懂了再写笔记),还有protobuf-net这个对数据进行序列化的工具。

    其实protobuf-net只是protobuf在.net平台上的应用,而protobuf是google提供的供数据序列化的工具,在网上搜索能找到一大堆相关资料,估计这个工具对数据的处理速度和节省空间的能力都很强,所以很多人才用这个工具。在http://code.google.com/p/protobuf/ 上面有详细的说明,加上我对JAVA不感兴趣,在此就不做什么介绍了。

    至于protobuf-net,则是由一个人进行开发的,这个程序员在自己的博客上贴出了很多资料,告诉人们怎么使用,地址在这里:http://marcgravell.blogspot.com/search/label/protobuf-net (需翻墙)http://code.google.com/p/protobuf-net/(这里是代码维护地址)

    下面来说说如何使用

    下载压缩包,解压后会发现有很多平台,至于我就需要选择windows phone平台,里面有三个文件

    先把protobuf-net.dll 添加到工程引用

    编写自定义的protobuf类,在我的程序中类(ProtoBuf.cs)如下:

    [csharp] view plaincopyprint?
     
    1. using ProtoBuf;  
    2. using System.Collections.Generic;  
    3.   
    4. namespace SyncControls  
    5. {  
    6.     [ProtoContract]  
    7.     public class PBCalendarItem  
    8.     {  
    9.         // required int64 pid = 1;  
    10.         [ProtoMember(1,IsRequired=true)]  
    11.         public long pid { get; set; }  
    12.   
    13.         // required string title = 2;  
    14.         [ProtoMember(2, IsRequired = true)]  
    15.         public string title { get; set; }  
    16.   
    17.         // required int64 created = 3;  
    18.         [ProtoMember(3, IsRequired = true)]  
    19.         public long created { get; set; }  
    20.   
    21.         // required int64 modified = 4;  
    22.         [ProtoMember(4, IsRequired = true)]  
    23.         public long modified { get; set; }  
    24.   
    25.         // optional int64 owner_id = 5;  
    26.         [ProtoMember(5, IsRequired = false)]  
    27.         public long owner_id { get; set; }  
    28.   
    29.         // optional string location = 6;  
    30.         [ProtoMember(6, IsRequired = false)]  
    31.         public string location { get; set; }  
    32.   
    33.         // optional string description = 7;  
    34.         [ProtoMember(7, IsRequired = false)]  
    35.         public string description { get; set; }  
    36.   
    37.         // optional string timezone = 8;  
    38.         [ProtoMember(8, IsRequired = false)]  
    39.         public string timezone { get; set; }  
    40.   
    41.         // optional int32 is_primary = 9;  
    42.         [ProtoMember(9, IsRequired = false)]  
    43.         public int is_primary { get; set; }  
    44.   
    45.         // optional int32 is_public = 10;  
    46.         [ProtoMember(10, IsRequired = false)]  
    47.         public int is_public { get; set; }  
    48.   
    49.         // optional int32 pub_status_only = 11;  
    50.         [ProtoMember(11, IsRequired = false)]  
    51.         public int pub_status_only { get; set; }  
    52.   
    53.         // optional int32 searchable = 12;  
    54.         [ProtoMember(12, IsRequired = false)]  
    55.         public int searchable { get; set; }  
    56.   
    57.         // optional string data_domain = 13;  
    58.         [ProtoMember(13, IsRequired = false)]  
    59.         public string data_domain { get; set; }  
    60.   
    61.         // optional int32 calendar_type = 14;  
    62.         [ProtoMember(14, IsRequired = false)]  
    63.         public int calendar_type { get; set; }  
    64.   
    65.         // optional string sync_state = 15;  
    66.         [ProtoMember(15, IsRequired = false)]  
    67.         public string sync_state { get; set; }  
    68.   
    69.         // optional int32 access_type = 16;  
    70.         [ProtoMember(16, IsRequired = false)]  
    71.         public int access_type { get; set; }  
    72.   
    73.         // optional string uuid = 17;  
    74.         [ProtoMember(17, IsRequired = false)]  
    75.         public string uuid { get; set; }  
    76.   
    77.         // optional string color = 18;  
    78.         [ProtoMember(18, IsRequired = false)]  
    79.         public string color { get; set; }  
    80.     }  
    81.       
    82.     [ProtoContract]  
    83.     public class PBScheduleItem  
    84.     {  
    85.         // required int64 pid = 1;  
    86.         [ProtoMember(1, IsRequired = true)]  
    87.         public long pid { get; set; }  
    88.   
    89.         // required int64 calendar_id = 2;  
    90.         [ProtoMember(2, IsRequired = true)]  
    91.         public long calendar_id { get; set; }  
    92.   
    93.         // required int64 created = 3;  
    94.         [ProtoMember(3, IsRequired = true)]  
    95.         public long created { get; set; }  
    96.   
    97.         // required int64 modified = 4;  
    98.         [ProtoMember(4, IsRequired = true)]  
    99.         public long modified { get; set; }  
    100.   
    101.         // required string calendar_type = 5;  
    102.         [ProtoMember(5, IsRequired = true)]  
    103.         public string calendar_type { get; set; }  
    104.   
    105.         // optional string sync_state = 6;  
    106.         [ProtoMember(6, IsRequired = false)]  
    107.         public string sync_state { get; set; }  
    108.   
    109.         // optional int64 owner_id = 7;  
    110.         [ProtoMember(7, IsRequired = false)]  
    111.         public long owner_id { get; set; }  
    112.   
    113.         // optional int64 start_time = 8;  
    114.         [ProtoMember(8, IsRequired = false)]  
    115.         public long start_time { get; set; }  
    116.   
    117.         // optional int32 duration = 9;  
    118.         [ProtoMember(9, IsRequired = false)]  
    119.         public int duration { get; set; }  
    120.   
    121.         // optional int32 allday_event = 10;  
    122.         [ProtoMember(10, IsRequired = false)]  
    123.         public int allday_event { get; set; }  
    124.   
    125.         // optional int32 access_type = 11;  
    126.         [ProtoMember(11, IsRequired = false)]  
    127.         public int access_type { get; set; }  
    128.   
    129.         // optional int32 status_busy = 12;  
    130.         [ProtoMember(12, IsRequired = false)]  
    131.         public int status_busy { get; set; }  
    132.   
    133.         // optional string title = 13;  
    134.         [ProtoMember(13, IsRequired = false)]  
    135.         public string title { get; set; }  
    136.   
    137.         // optional string location = 14;  
    138.         [ProtoMember(14, IsRequired = false)]  
    139.         public string location { get; set; }  
    140.   
    141.         // optional string description = 15;  
    142.         [ProtoMember(15, IsRequired = false)]  
    143.         public string description { get; set; }  
    144.   
    145.         // optional string url = 16;  
    146.         [ProtoMember(16, IsRequired = false)]  
    147.         public string url { get; set; }  
    148.   
    149.         // optional int32 check_completed = 17;  
    150.         [ProtoMember(17, IsRequired = false)]  
    151.         public int check_completed { get; set; }  
    152.   
    153.         // optional int32 repeat_type = 18;  
    154.         [ProtoMember(18, IsRequired = false)]  
    155.         public int repeat_type { get; set; }  
    156.   
    157.         // optional int32 repeat_finished = 19;  
    158.         [ProtoMember(19, IsRequired = false)]  
    159.         public int repeat_finished { get; set; }  
    160.   
    161.         // optional int64 repeat_stop_time = 20;  
    162.         [ProtoMember(20, IsRequired = false)]  
    163.         public long repeat_stop_time { get; set; }  
    164.   
    165.         // optional int32 repeat_count = 21;  
    166.         [ProtoMember(21, IsRequired = false)]  
    167.         public int repeat_count { get; set; }  
    168.   
    169.         // optional int32 repeat_frequency = 22;  
    170.         [ProtoMember(22, IsRequired = false)]  
    171.         public int repeat_frequency { get; set; }  
    172.   
    173.         // optional string repeat_month = 23;  
    174.         [ProtoMember(23, IsRequired = false)]  
    175.         public string repeat_month { get; set; }  
    176.   
    177.         // optional string repeat_monthday = 24;  
    178.         [ProtoMember(24, IsRequired = false)]  
    179.         public string repeat_monthday { get; set; }  
    180.   
    181.         // optional string repeat_day = 25;  
    182.         [ProtoMember(25, IsRequired = false)]  
    183.         public string repeat_day { get; set; }  
    184.   
    185.         // optional string repeat_weekno = 26;  
    186.         [ProtoMember(26, IsRequired = false)]  
    187.         public string repeat_weekno { get; set; }  
    188.   
    189.         // optional string repeat_yearday = 27;  
    190.         [ProtoMember(27, IsRequired = false)]  
    191.         public string repeat_yearday { get; set; }  
    192.   
    193.         // optional string timezone = 28;  
    194.         [ProtoMember(28, IsRequired = false)]  
    195.         public string timezone { get; set; }  
    196.   
    197.         // optional string uuid = 29;  
    198.         [ProtoMember(29, IsRequired = false)]  
    199.         public string uuid { get; set; }  
    200.   
    201.         // optional string calendar_uuid = 30;  
    202.         [ProtoMember(30, IsRequired = false)]  
    203.         public string calendar_uuid { get; set; }  
    204.   
    205.         [ProtoMember(31, IsRequired = false)]  
    206.         public List<PBScheduleExceptionItem> scheduleExceptions { get; private set; }  
    207.   
    208.         [ProtoMember(32, IsRequired = false)]  
    209.         public List<PBScheduleAlarmItem> scheduleAlarms { get; private set; }  
    210.   
    211.   
    212.         [ProtoMember(33, IsRequired = false)]  
    213.         public List<PBScheduleUserItem> scheduleUsers { get; private set; }  
    214.   
    215.   
    216.         [ProtoMember(34, IsRequired = false)]  
    217.         public List<PBScheduleShareItem> scheduleShares { get; private set; }  
    218.   
    219.         public PBScheduleItem()  
    220.         {  
    221.             scheduleExceptions = new List<PBScheduleExceptionItem>();  
    222.             scheduleAlarms = new List<PBScheduleAlarmItem>();  
    223.             scheduleShares = new List<PBScheduleShareItem>();  
    224.             scheduleUsers = new List<PBScheduleUserItem>();  
    225.         }  
    226.     }  
    227.   
    228.     [ProtoContract]  
    229.     public class PBScheduleExceptionItem  
    230.     {  
    231.         // required int64 pid = 1;  
    232.         [ProtoMember(1, IsRequired = true)]  
    233.         public long pid { get; set; }  
    234.   
    235.         // required int32 schedule_id = 2;  
    236.         [ProtoMember(2, IsRequired = true)]  
    237.         public int schedule_id { get; set; }  
    238.   
    239.         // required string excep_date = 3;  
    240.         [ProtoMember(3, IsRequired = true)]  
    241.         public string excep_date { get; set; }  
    242.   
    243.         // optional string sync_state = 4;  
    244.         [ProtoMember(4, IsRequired = false)]  
    245.         public string sync_state { get; set; }  
    246.     }  
    247.   
    248.     [ProtoContract]  
    249.     public class PBScheduleAlarmItem  
    250.     {  
    251.         // required int64 pid = 1;  
    252.         [ProtoMember(1, IsRequired = true)]  
    253.         public long pid { get; set; }  
    254.   
    255.         // optional int64 created = 2;  
    256.         [ProtoMember(2, IsRequired = false)]  
    257.         public long created { get; set; }  
    258.   
    259.         // optional int64 modified = 3;  
    260.         [ProtoMember(3, IsRequired = false)]  
    261.         public long modified { get; set; }  
    262.   
    263.         // required int64 schedule_id = 4;  
    264.         [ProtoMember(4, IsRequired = true)]  
    265.         public long schedule_id { get; set; }  
    266.   
    267.         // required int64 user_id = 5;  
    268.         [ProtoMember(5, IsRequired = true)]  
    269.         public long user_id { get; set; }  
    270.   
    271.         // required int64 alarm_id = 6;  
    272.         [ProtoMember(6, IsRequired = true)]  
    273.         public long alarm_id { get; set; }  
    274.   
    275.         // required int32 before_minutes = 7;  
    276.         [ProtoMember(7, IsRequired = true)]  
    277.         public int before_minutes { get; set; }  
    278.   
    279.         // optional int64 next_alarm = 8;  
    280.         [ProtoMember(8, IsRequired = false)]  
    281.         public long next_alarm { get; set; }  
    282.   
    283.         // optional string sync_state = 9;  
    284.         [ProtoMember(9, IsRequired = false)]  
    285.         public string sync_state { get; set; }  
    286.     }  
    287.   
    288.     [ProtoContract]  
    289.     public class PBScheduleUserItem  
    290.     {  
    291.         // required int64 pid = 1;  
    292.         [ProtoMember(1, IsRequired = true)]  
    293.         public long pid { get; set; }  
    294.   
    295.         // required int64 schedule_id = 2;  
    296.         [ProtoMember(2, IsRequired = true)]  
    297.         public long schedule_id { get; set; }  
    298.   
    299.         // required int64 user_id = 3;  
    300.         [ProtoMember(3, IsRequired = true)]  
    301.         public long user_id { get; set; }  
    302.   
    303.         // required int32 is_owner = 4;  
    304.         [ProtoMember(4, IsRequired = true)]  
    305.         public int is_owner { get; set; }  
    306.   
    307.         // required int32 participant_status = 5;  
    308.         [ProtoMember(5, IsRequired = true)]  
    309.         public int participant_status { get; set; }  
    310.   
    311.         // optional int32 allow_edit = 6;  
    312.         [ProtoMember(6, IsRequired = false)]  
    313.         public int allow_edit { get; set; }  
    314.   
    315.         // optional int32 allow_invite_others = 7;  
    316.         [ProtoMember(7, IsRequired = false)]  
    317.         public int allow_invite_others { get; set; }  
    318.   
    319.         // optional int32 allow_view_participants = 8;  
    320.         [ProtoMember(8, IsRequired = false)]  
    321.         public int allow_view_participants { get; set; }  
    322.   
    323.         // optional string participant_comment = 9;  
    324.         [ProtoMember(9, IsRequired = false)]  
    325.         public string participant_comment { get; set; }  
    326.   
    327.         // optional string sync_state = 10;  
    328.         [ProtoMember(10, IsRequired = false)]  
    329.         public string sync_state { get; set; }  
    330.     }  
    331.   
    332.     [ProtoContract]  
    333.     public class PBScheduleShareItem  
    334.     {  
    335.         // required int64 pid = 1;  
    336.         [ProtoMember(1, IsRequired = true)]  
    337.         public long pid { get; set; }  
    338.   
    339.         // optional int64 created = 2;  
    340.         [ProtoMember(2, IsRequired = false)]  
    341.         public long created { get; set; }  
    342.   
    343.         // optional int64 modified = 3;  
    344.         [ProtoMember(3, IsRequired = false)]  
    345.         public long modified { get; set; }  
    346.   
    347.         // optional string sync_state = 4;  
    348.         [ProtoMember(4, IsRequired = false)]  
    349.         public string sync_state { get; set; }  
    350.   
    351.         // required int64 schedule_id = 5;  
    352.         [ProtoMember(5, IsRequired = true)]  
    353.         public long schedule_id { get; set; }  
    354.   
    355.         // required int64 contact_id = 6;  
    356.         [ProtoMember(6, IsRequired = true)]  
    357.         public long contact_id { get; set; }  
    358.   
    359.         // optional string fullname = 7;  
    360.         [ProtoMember(7, IsRequired = false)]  
    361.         public string fullname { get; set; }  
    362.   
    363.         // optional string mobile = 8;  
    364.         [ProtoMember(8, IsRequired = false)]  
    365.         public string mobile { get; set; }  
    366.   
    367.         // optional string send_state = 9;  
    368.         [ProtoMember(9, IsRequired = false)]  
    369.         public string send_state { get; set; }  
    370.     }  
    371.   
    372.     [ProtoContract]  
    373.     public class PBNoteItem  
    374.     {  
    375.         // required int64 pid = 1;  
    376.         [ProtoMember(1, IsRequired = true)]  
    377.         public long pid { get; set; }  
    378.   
    379.         // required int64 created = 2;  
    380.         [ProtoMember(2, IsRequired = true)]  
    381.         public long created { get; set; }  
    382.   
    383.         // required int64 modified = 3;  
    384.         [ProtoMember(3, IsRequired = true)]  
    385.         public long modified { get; set; }  
    386.   
    387.         // optional string sync_state = 4;  
    388.         [ProtoMember(4, IsRequired = false)]  
    389.         public string sync_state { get; set; }  
    390.   
    391.         // optional string title = 5;  
    392.         [ProtoMember(5, IsRequired = false)]  
    393.         public string title { get; set; }  
    394.   
    395.         // optional int64 owner_id = 6;  
    396.         [ProtoMember(6, IsRequired = false)]  
    397.         public long owner_id { get; set; }  
    398.   
    399.         // optional int32 state = 7;  
    400.         [ProtoMember(7, IsRequired = false)]  
    401.         public int state { get; set; }  
    402.   
    403.         // optional int32 priority = 8;  
    404.         [ProtoMember(8, IsRequired = false)]  
    405.         public int priority { get; set; }  
    406.   
    407.         // optional int64 finish_time = 9;  
    408.         [ProtoMember(9, IsRequired = false)]  
    409.         public long finish_time { get; set; }  
    410.   
    411.         // optional int64 deadline = 10;  
    412.         [ProtoMember(10, IsRequired = false)]  
    413.         public long deadline { get; set; }  
    414.   
    415.         // optional string tags = 11;  
    416.         [ProtoMember(11, IsRequired = false)]  
    417.         public string tags { get; set; }  
    418.   
    419.         // optional int32 seq = 12;  
    420.         [ProtoMember(12, IsRequired = false)]  
    421.         public int seq { get; set; }  
    422.   
    423.         // optional string uuid = 13;  
    424.         [ProtoMember(13, IsRequired = false)]  
    425.         public string uuid { get; set; }  
    426.     }  
    427. }  


    再编写转换类,这里是PBConverter.cs

    [csharp] view plaincopyprint?
     
    1. using DataBase;  
    2. using System;  
    3. using DataManager;  
    4. using Utilities;  
    5.   
    6. namespace SyncControls  
    7. {  
    8.     public static class DataBaseExMethods  
    9.     {  
    10.         #region Calendar  
    11.         public static PBCalendarItem ToProtoBuf(this CalendarItem t)  
    12.         {  
    13.             PBCalendarItem item = new PBCalendarItem();  
    14.             item.pid = t.Id;  
    15.             item.title = t.Title;  
    16.             item.created = t.Created;  
    17.             item.modified = t.Modified;  
    18.             item.owner_id = t.OwnerId;  
    19.             item.location = t.Location;  
    20.             item.description = t.Description;  
    21.             item.timezone = t.TimeZone;  
    22.             item.is_primary = t.IsPrimary ? 1 : 0;  
    23.             item.is_public = t.IsPublic ? 1 : 0;  
    24.             item.pub_status_only = t.PubStatusOnly ? 1 : 0;  
    25.             item.searchable = t.Searchable ? 1 : 0;  
    26.             item.data_domain = t.DataDomain;  
    27.             item.calendar_type = t.CalendarType;  
    28.             item.access_type = t.AccessType;  
    29.             item.sync_state = t.SyncState;  
    30.             item.uuid = t.UUID;  
    31.             item.color = t.Color;  
    32.             return item;  
    33.         }  
    34.   
    35.         public static void FromProtoBuf(this CalendarItem t, PBCalendarItem item)  
    36.         {  
    37.             t.Id = item.pid;  
    38.             t.Title = item.title;  
    39.             t.Created = item.created;  
    40.             t.Modified = item.modified;  
    41.             t.OwnerId = item.owner_id;  
    42.             t.Location = item.location;  
    43.             t.Description = item.description;  
    44.             t.TimeZone = item.timezone;  
    45.             t.IsPrimary = item.is_primary == 1;  
    46.             t.IsPublic = item.is_public == 1;  
    47.             t.PubStatusOnly = item.pub_status_only == 1;  
    48.             t.Searchable = item.searchable == 1;  
    49.             t.DataDomain = item.data_domain;  
    50.             t.CalendarType = item.calendar_type;  
    51.             t.AccessType = item.access_type;  
    52.             t.SyncState = item.sync_state;  
    53.             t.UUID = item.uuid;  
    54.             t.Color = item.color;  
    55.         }  
    56.         #endregion  
    57.  
    58.         #region Note  
    59.         public static PBNoteItem ToProtoBuf(this NoteItem t)  
    60.         {  
    61.             PBNoteItem item = new PBNoteItem();  
    62.             item.pid = t.Id;  
    63.             item.title = t.Title;  
    64.             item.created = t.Created;  
    65.             item.modified = t.Modified;  
    66.             item.sync_state = t.SyncState;  
    67.             item.title = t.Title;  
    68.             item.owner_id = t.OwnerId;  
    69.             item.state = t.State;  
    70.             item.priority = t.Priority;  
    71.             item.finish_time = t.FinishTime;  
    72.             item.deadline = t.Deadline;  
    73.             item.tags = t.Tags;  
    74.             item.seq = t.Seq;  
    75.             item.uuid = t.UUID;  
    76.             return item;  
    77.         }  
    78.   
    79.         public static void FromProtoBuf(this NoteItem t, PBNoteItem item)  
    80.         {  
    81.             t.Id = item.pid;  
    82.             t.Title = item.title;  
    83.             t.Created = item.created;  
    84.             t.Modified = item.modified;  
    85.             t.SyncState = item.sync_state;  
    86.             t.Title = item.title;  
    87.             t.OwnerId = item.owner_id;  
    88.             t.State = item.state;  
    89.             t.Priority = item.priority;  
    90.             t.FinishTime = item.finish_time;  
    91.             t.Deadline = item.deadline;  
    92.             t.Tags = item.tags;  
    93.             t.Seq = item.seq;  
    94.             t.UUID = item.uuid;  
    95.         }  
    96.         #endregion  
    97.  
    98.         #region Schedule  
    99.         public static PBScheduleItem ToProtoBuf(this ScheduleItem t)  
    100.         {  
    101.             PBScheduleItem item = new PBScheduleItem();  
    102.             item.pid = t.Id;  
    103.             item.calendar_id = t.CalendarId;  
    104.             item.created = t.Created;  
    105.             item.modified = t.Modified;  
    106.             item.calendar_type = t.CalendarType;  
    107.             item.sync_state = t.SyncState;  
    108.             item.owner_id = t.OwnerId;  
    109.             item.start_time = UnixTimestampConverter.ConvertLocalToTimestamp(t.StartTime);  
    110.             item.duration = t.Duration;  
    111.             item.allday_event = t.AllDayEvent ? 1 : 0;  
    112.             item.access_type = t.AccessType;  
    113.             item.status_busy = t.StatusBusy ? 1 : 0;  
    114.             item.title = t.Title;  
    115.             item.location = t.Location;  
    116.             item.description = t.Description;  
    117.             item.url = t.Url;  
    118.             item.check_completed = t.Completed ? 1 : 0;  
    119.             item.repeat_type = t.RepeatType;  
    120.             item.repeat_finished = t.RepeatFinished ? 1 : 0;  
    121.             item.repeat_stop_time = t.RepeatStopTime;  
    122.             item.repeat_count = t.RepeatCount;  
    123.             item.repeat_frequency = t.RepeatFrequency;  
    124.             item.repeat_month = t.RepeatMonth;  
    125.             item.repeat_monthday = t.RepeatMonthDay;  
    126.             item.repeat_day = t.RepeatDay;  
    127.             item.repeat_weekno = t.RepeatWeekNumber;  
    128.             item.repeat_yearday = t.RepeatYearDay;  
    129.             item.timezone = t.TimeZone;  
    130.             item.uuid = t.UUID;  
    131.             item.calendar_uuid = CalendarManager.GetCalendarUUID(t.CalendarId);  
    132.             if (item.calendar_uuid == "")  
    133.             {  
    134.                 return null;  
    135.             }  
    136.   
    137.             return item;  
    138.         }  
    139.   
    140.         public static void FromProtoBuf(this ScheduleItem t, PBScheduleItem item)  
    141.         {  
    142.             t.Id = item.pid;  
    143.             t.CalendarId = CalendarManager.GetCalendarID(item.calendar_uuid);  
    144.             t.Created = item.created;  
    145.             t.Modified = item.modified;  
    146.             t.CalendarType = item.calendar_type;  
    147.             t.SyncState = item.sync_state;  
    148.             t.OwnerId = item.owner_id;  
    149.             t.StartTime = UnixTimestampConverter.ConvertLocalFromTimestamp(item.start_time);  
    150.             t.Duration = item.duration;  
    151.             t.AllDayEvent = item.allday_event == 1;  
    152.             t.AccessType = item.access_type;  
    153.             t.StatusBusy = item.status_busy == 1;  
    154.             t.Title = item.title;  
    155.             t.Location = item.location;  
    156.             t.Description = item.description;  
    157.             t.Url = item.url;  
    158.             t.CheckCompleted = item.check_completed == 1;  
    159.             t.RepeatType = item.repeat_type;  
    160.             t.RepeatFinished = item.repeat_finished == 1;  
    161.             t.RepeatStopTime = item.repeat_stop_time;  
    162.             t.RepeatCount = item.repeat_count;  
    163.             t.RepeatFrequency = item.repeat_frequency;  
    164.             t.RepeatMonth = item.repeat_month;  
    165.             t.RepeatMonthDay = item.repeat_monthday;  
    166.             t.RepeatDay = item.repeat_day;  
    167.             t.RepeatWeekNumber = item.repeat_weekno;  
    168.             t.RepeatYearDay = item.repeat_yearday;  
    169.             t.TimeZone = item.timezone;  
    170.             t.UUID = item.uuid;  
    171.         }  
    172.         #endregion  
    173.  
    174.         #region ScheduleAlarm  
    175.         public static PBScheduleAlarmItem ToProtoBuf(this ScheduleAlarmItem t)  
    176.         {  
    177.             PBScheduleAlarmItem item = new PBScheduleAlarmItem();  
    178.             item.pid = t.Id;  
    179.             item.created = t.Created;  
    180.             item.modified = t.Modified;  
    181.             item.sync_state = t.SyncState;  
    182.             item.schedule_id = t.ScheduleId;  
    183.             item.user_id = t.UserId;  
    184.             item.alarm_id = t.AlarmId;  
    185.             item.before_minutes = t.BeforeMinutes;  
    186.             return item;  
    187.         }  
    188.   
    189.         public static void FromProtoBuf(this ScheduleAlarmItem t,PBScheduleAlarmItem item)  
    190.         {  
    191.             t.Id = item.pid;  
    192.             t.Created = item.created;  
    193.             t.Modified = item.modified;  
    194.             t.SyncState = item.sync_state;  
    195.             t.ScheduleId = item.schedule_id;  
    196.             t.UserId = item.user_id;  
    197.             t.AlarmId = (int)item.alarm_id;  
    198.             t.BeforeMinutes = item.before_minutes;  
    199.         }  
    200.         #endregion  
    201.  
    202.         #region ScheduleException  
    203.         public static PBScheduleExceptionItem ToProtoBuf(this ScheduleExceptionItem t)  
    204.         {  
    205.             PBScheduleExceptionItem item = new PBScheduleExceptionItem();  
    206.             item.pid = t.Id;  
    207.             item.sync_state = t.SyncState;  
    208.             item.schedule_id = (int)t.ScheduleId;  
    209.             item.excep_date = Convert.ToString(t.ExceptDate);  
    210.             return item;  
    211.         }  
    212.   
    213.         public static void FromProtoBuf(this ScheduleExceptionItem t, PBScheduleExceptionItem item)  
    214.         {  
    215.             t.Id = item.pid;  
    216.             t.SyncState = item.sync_state;  
    217.             t.ScheduleId = item.schedule_id;  
    218.             t.ExceptDate = Convert.ToInt64(item.excep_date);  
    219.         }  
    220.         #endregion  
    221.  
    222.         #region ScheduleShare  
    223.         public static PBScheduleShareItem ToProtoBuf(this ScheduleShareItem t)  
    224.         {  
    225.             PBScheduleShareItem item = new PBScheduleShareItem();  
    226.             item.pid = t.Id;  
    227.             item.created = t.Created;  
    228.             item.modified = t.Modified;  
    229.             item.sync_state = t.SyncState;  
    230.             item.schedule_id = t.ScheduleId;  
    231.             item.fullname = t.FullName;  
    232.             item.mobile = t.Mobile;  
    233.             item.send_state = t.SendState;  
    234.             return item;  
    235.         }  
    236.   
    237.         public static void FromProtoBuf(this ScheduleShareItem t, PBScheduleShareItem item)  
    238.         {  
    239.             t.Id = item.pid;  
    240.             t.Created = item.created;  
    241.             t.Modified = item.modified;  
    242.             t.SyncState = item.sync_state;  
    243.             t.ScheduleId = item.schedule_id;  
    244.             t.FullName = item.fullname;  
    245.             t.Mobile = item.mobile;  
    246.             t.SendState = item.send_state;  
    247.         }  
    248.         #endregion  
    249.  
    250.         #region ScheduleUser  
    251.         public static PBScheduleUserItem ToProtoBuf(this ScheduleUserItem t)  
    252.         {  
    253.             PBScheduleUserItem item = new PBScheduleUserItem();  
    254.             item.pid = t.Id;  
    255.             item.schedule_id = t.ScheduleId;  
    256.             item.user_id = t.UserId;  
    257.             item.is_owner = t.IsOwner ? 1 : 0;  
    258.             item.participant_status = t.ParticipantStatus;  
    259.             item.allow_edit = t.AllowEdit ? 1 : 0;  
    260.             item.allow_invite_others = t.AllowInviteOthers ? 1 : 0;  
    261.             item.allow_view_participants = t.AllowViewParticipants ? 1 : 0;  
    262.             item.participant_comment = t.ParticipantComment;  
    263.             item.sync_state = t.SyncState;  
    264.             return item;  
    265.         }  
    266.   
    267.         public static void FromProtoBuf(this ScheduleUserItem t, PBScheduleUserItem item)  
    268.         {  
    269.             t.Id = item.pid;  
    270.             t.ScheduleId = item.schedule_id;  
    271.             t.UserId = item.user_id;  
    272.             t.IsOwner = item.is_owner == 1;  
    273.             t.ParticipantStatus = item.participant_status;  
    274.             t.AllowEdit = item.allow_edit == 1;  
    275.             t.AllowInviteOthers = item.allow_invite_others == 1;  
    276.             t.AllowViewParticipants = item.allow_view_participants == 1;  
    277.             t.ParticipantComment = item.participant_comment;  
    278.             t.SyncState = item.sync_state;  
    279.         }  
    280.         #endregion  
    281.     }  
    282. }  

    使用的时候,只需要按照下面方式写即可

    [csharp] view plaincopyprint?
     
    1. protected override SyncItem GetItemContent(SyncItem item)  
    2. {  
    3.     CalendarItem t = CalendarManager.GetCalendarByID(Convert.ToInt64(item.Key));  
    4.     PBCalendarItem pbItem = t.ToProtoBuf();  
    5.     return ToSyncItem(pbItem);  
    6. }  
    7.   
    8. private SyncItem ToSyncItem(PBCalendarItem pbItem)  
    9. {  
    10.     SyncItem item = new SyncItem(Convert.ToString(pbItem.pid));  
    11.     string syncState = pbItem.sync_state;   
    12.     MemoryStream stream = new MemoryStream();  
    13.           //序列化  
    14.     Serializer.Serialize<PBCalendarItem>(stream, pbItem);  
    15.     item.SetContent(stream.GetBuffer());  
    16.     return item;  
    17. }  
    18.   
    19. private PBCalendarItem FromSyncItem(SyncItem item)  
    20. {  
    21.     byte[] b = item.GetContent();  
    22.     PBCalendarItem pbItem = null;  
    23.     try  
    24.     {  
    25.         MemoryStream stream = new MemoryStream(b);  
    26.               //反序列化  
    27.         pbItem = Serializer.Deserialize<PBCalendarItem>(stream);  
    28.     }  
    29.     catch { }  
    30.     return pbItem;  
    31. }  

    这里是把数据写入到一个内存流里面,而不是通常意义上的文件之中,如果是文件可以看这里的例子

    http://hi.baidu.com/jack1865/item/09a3f8ef4c8505f2eb34c957

    以上只是对这个工具的简单使用说明而已,现在protobuf-net的作者已经开发出了更强的序列号工具,通过他给的precompiler工具加上自己写的特定框架下的试验工程,编译得到适合该特定框架下的序列化工具。

    precompile {some path}PhoneDto.dll –o:PhoneSerializer.dll –t:MySerializer

    Then, just use MySerializer:

    var ser = new MySerializer();
    ser.Serialize(output, obj);

    看来还有很多知识要学习。

  • 相关阅读:
    校门外的树
    学生档案
    冒泡排序法
    寻找最大数序列
    初识结构体
    找零钱
    冒泡的应用
    关于数组的逆序重放
    关于质数
    字符串转换为日期格式
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4228588.html
Copyright © 2011-2022 走看看