{"id":3064,"date":"2019-12-20T14:11:58","date_gmt":"2019-12-20T13:11:58","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3064"},"modified":"2022-07-25T08:33:24","modified_gmt":"2022-07-25T07:33:24","slug":"c-dictionarymodel","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-dictionarymodel\/","title":{"rendered":"C# DictionaryModel"},"content":{"rendered":"\n<p>For extending JSON or dynamic types with optional fields<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class DictionaryModel\n{\n\tprivate Dictionary&lt;string, object> _data = new Dictionary&lt;string, object>();\n\tpublic void SetDictionary(Dictionary&lt;string, object> data) \n\t{\n\t\t_data = data;\n\t}\n\tpublic Dictionary&lt;string, object> GetDictionary() {\n\t\treturn _data;\n\t}\n\n\tprotected T Get&lt;T>(string key, T defaulValue = default(T)) {\n\t\tif (_data.TryGetValue(key, out var val)) {\n\t\t\tif (val == null)\n\t\t\t\treturn defaulValue;\n\t\t\tif (val is T)\n\t\t\t\treturn (T)val;\n\n\t\t\tvar basicType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);\n\t\t\ttry\n\t\t\t{\n\t\t\t\tvar basicVal = Convert.ChangeType(val, basicType);\n\t\t\t\treturn (T)basicVal;\n\t\t\t}\n\t\t\tcatch (Exception)\n\t\t\t{\n\t\t\t\t\/\/Ignore\n\t\t\t}\n\t\t}\n\t\treturn defaulValue;\n\t}\n\n\tprotected void Set(string key, object value) {\n\t\t_data[key] = value;\n\t}\n}\n\npublic class ModelWithDescription : DictionaryModel\n{\n\tpublic string Description\n\t{\n\t\tget => Get&lt;string>(\"Description\");\n\t\tset => Set(\"Description\", value);\n\t}\n\tpublic int? Quantity\n\t{\n\t\tget => Get&lt;int?>(\"Quantity\");\n\t\tset => Set(\"Quantity\", value);\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For extending JSON or dynamic types with optional fields<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-3064","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3064"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3064\/revisions"}],"predecessor-version":[{"id":3065,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3064\/revisions\/3065"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}