{"id":246591,"date":"2022-07-13T08:39:15","date_gmt":"2022-07-13T00:39:15","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=246591"},"modified":"2022-06-29T09:41:43","modified_gmt":"2022-06-29T01:41:43","slug":"linux-messasge-pack","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-messasge-pack.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdC#\u4f7f\u7528\u5bf9\u8c61\u5e8f\u5217\u5316\u7c7b\u5e93MessasgePack"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86C#\u4f7f\u7528\u5bf9\u8c61\u5e8f\u5217\u5316\u7c7b\u5e93MessasgePack\u7684\u65b9\u6cd5\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\u3002\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

MessagePack\u662f\u4e00\u79cd\u9ad8\u6548\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u683c\u5f0f\u3002\u53ef\u4ee5\u5728\u591a\u79cd\u8bed\u8a00\u4e2d\u8fdb\u884c\u5feb\u901f\u6570\u636e\u4ea4\u6362\uff0c\u6bd4\u5982JSON\u683c\u5f0f\u7b49\u3002\u5b83\u6bd4Json\u66f4\u52a0\u5c0f\u5de7\uff0c\u66f4\u52a0\u9ad8\u6548\uff0c\u53ef\u4ee5\u7528\u4e8e\u4e00\u4e9b\u7ed3\u6784\u5316\u6570\u636e\u5b58\u50a8 \uff0c\u975e\u5e38\u9002\u5408\u9002\u7528\u4e8e\u6d88\u606f\u603b\u7ebf\uff0cMemoryCache\u7b49\u5bf9\u5e8f\u5217\u5316\u8981\u6c42\u6bd4\u8f83\u9ad8\u7684\u573a\u5408\u3002<\/p>\n

MessagePack\u57fa\u672c\u4e0a\u88ab\u5927\u591a\u6570\u4e3b\u6d41\u8bed\u8a00\u652f\u6301\uff0c\u56e0\u6b64\u7528\u5b83\u6765\u4f5c\u4e3a\u6559\u7a0b\u95f4\u4ea4\u6362\u7684\u6d88\u606f\u5e8f\u5217\u5316\u4e5f\u662f\u4e00\u79cd\u4e0d\u9519\u7684\u65b9\u5f0f\uff0c\u672c\u6587\u8fd9\u91cc\u7b80\u5355\u7684\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u5728.Net \u4e2d\u4f7f\u7528MessagePack\u3002<\/p>\n

\u9996\u5148\u5b89\u88c5MessagePack\u548cMessagePackAnalyzer\u7684Nuget\u5305\uff1a<\/p>\n

Install-Package MessagePack\r\nInstall-Package MessagePackAnalyzer<\/pre>\n

\u5176\u4e2dMessagePackAnalyzer\u662f\u53ef\u9009\u7684\uff0c\u4f46\u5b83\u53ef\u4ee5\u5e2e\u52a9\u68c0\u67e5\u5e8f\u5217\u5316\u6807\u8bb0\u7684\u6b63\u786e\u6027\uff0c\u8fd8\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n

\u5982\u4e0b\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u975e\u5e38\u7b80\u5355\uff0c\u6211\u5c31\u4e0d\u591a\u4ecb\u7ecd\u4e86\u3002 <\/p>\n

\r\n\/\/ mark MessagePackObjectAttribute\r\n[MessagePackObject]\r\npublic class MyClass\r\n{\r\n    \/\/ Key is serialization index, it is important for versioning.\r\n    [Key(0)]\r\n    public int Age { get; set; }\r\n \r\n    [Key(1)]\r\n    public string FirstName { get; set; }\r\n \r\n    [Key(2)]\r\n    public string LastName { get; set; }\r\n \r\n    \/\/ public members and does not serialize target, mark IgnoreMemberttribute\r\n    [IgnoreMember]\r\n    public string FullName { get { return FirstName + LastName; } }\r\n}\r\n \r\nclass Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        var mc = new MyClass\r\n        {\r\n            Age = 99,\r\n            FirstName = \"hoge\",\r\n            LastName = \"huga\",\r\n        };\r\n \r\n        \/\/ call Serialize\/Deserialize, that's all.\r\n        var bytes = MessagePackSerializer.Serialize(mc);\r\n        var mc2 = MessagePackSerializer.Deserialize(bytes);\r\n \r\n        \/\/ you can dump msgpack binary to human readable json.\r\n        \/\/ In default, MeesagePack for C# reduce property name information.\r\n        \/\/ [99,\"hoge\",\"huga\"]\r\n        var json = MessagePackSerializer.ToJson(bytes);\r\n        Console.WriteLine(json);\r\n    }\r\n}<\/myclass><\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eC#\u4f7f\u7528\u5bf9\u8c61\u5e8f\u5217\u5316\u7c7b\u5e93MessasgePack\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

MessagePack\u662f\u4e00\u79cd\u9ad8\u6548\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u683c\u5f0f\u3002\u53ef\u4ee5\u5728\u591a\u79cd\u8bed\u8a00\u4e2d\u8fdb\u884c\u5feb\u901f\u6570\u636e\u4ea4\u6362\uff0c\u6bd4\u5982JSON\u683c\u5f0f\u7b49\u3002\u5b83\u6bd4J […]<\/p>\n","protected":false},"author":362,"featured_media":4213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-246591","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/246591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=246591"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/246591\/revisions"}],"predecessor-version":[{"id":246593,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/246591\/revisions\/246593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/4213"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=246591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=246591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=246591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}