{"id":234,"date":"2015-11-22T21:49:57","date_gmt":"2015-11-22T17:49:57","guid":{"rendered":"http:\/\/infobs.athn.ru\/?p=234"},"modified":"2015-11-22T21:49:57","modified_gmt":"2015-11-22T17:49:57","slug":"enum-to-dictionary-s-lokalizatsiey","status":"publish","type":"post","link":"https:\/\/infobase.athn.ru\/program\/2015\/11\/enum-to-dictionary-s-lokalizatsiey\/","title":{"rendered":"Enum to Dictionary \u0441 \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439"},"content":{"rendered":"<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e<br \/>\nhttp:\/\/adamyan.blogspot.com\/2010\/02\/aspnet-mvc-2-localization-complete.html<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c enum \u043a dictionary \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u0435\u043a<br \/>\nhttp:\/\/stackoverflow.com\/questions\/5583717\/enum-to-dictionary-c-sharp<\/p>\n<pre>\nvar dict = Enum.GetValues(typeof(typFoo))\n               .Cast()\n               .ToDictionary(t =&gt; (int)t, t =&gt; t.ToString() );\n<\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 generic-\u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0434\u043b\u044f enum<br \/>\nhttp:\/\/stackoverflow.com\/questions\/79126\/create-generic-method-constraining-t-to-an-enum<\/p>\n<pre>\npublic T GetEnumFromString(string value) where T : struct, IConvertible\n{\n   if (!typeof(T).IsEnum) \n   {\n      throw new ArgumentException(\"T must be an enumerated type\");\n   }\n\n   \/\/...\n}\n<\/pre>\n<pre>\npublic abstract class EnumClassUtils\nwhere TClass : class\n{\n\n    public static TEnum Parse(string value)\n    where TEnum : struct, TClass\n    {\n        return (TEnum) Enum.Parse(typeof(TEnum), value);\n    }\n\n}\n\npublic class EnumUtils : EnumClassUtils\n{\n}\n<\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0432\u043e\u0435\u0434\u0438\u043d\u043e &#8212; generic-\u043c\u0435\u0442\u043e\u0434, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0449\u0438\u0439 enum \u043a \u0441\u043b\u043e\u0432\u0430\u0440\u044e \u043d\u0430 \u043d\u0443\u0436\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. \u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u043d\u0430\u0434\u043e \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<pre>\npublic static Dictionary TranslateEnum() where TEnum : struct, IConvertible\n        {\n            if (!typeof(TEnum).IsEnum)\n            {\n                throw new ArgumentException(\"TEnum must be an enumerated type\");\n            }\n\n            var lang = typeof(MyLangStrings);\n\n            var dict = Enum.GetValues(typeof(TEnum))\n               .Cast()\n               .ToDictionary(t =&gt; (int)Enum.Parse(typeof(TEnum), t.ToString()), t =&gt;\n               {\n                   var p = lang.GetProperty(t.ToString());\n                   return p.GetValue(p.DeclaringType, null).ToString();\n               });\n            return dict;\n        }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e http:\/\/adamyan.blogspot.com\/2010\/02\/aspnet-mvc-2-localization-complete.html \u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c enum \u043a dictionary \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u0435\u043a http:\/\/stackoverflow.com\/questions\/5583717\/enum-to-dictionary-c-sharp var dict = Enum.GetValues(typeof(typFoo)) .Cast() .ToDictionary(t =&gt; (int)t, t =&gt; t.ToString() ); \u041f\u0440\u0438\u043c\u0435\u0440 \u0441 generic-\u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0434\u043b\u044f <a class=\"more-link\" href=\"https:\/\/infobase.athn.ru\/program\/2015\/11\/enum-to-dictionary-s-lokalizatsiey\/\">Continue Reading \u2192<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[72,78,81,218],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-dictionary","tag-enum","tag-generic","tag-lokalizatsiya"],"_links":{"self":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":0,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"wp:attachment":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}