{"id":284,"date":"2015-11-28T20:23:40","date_gmt":"2015-11-28T16:23:40","guid":{"rendered":"http:\/\/infobs.athn.ru\/?p=284"},"modified":"2015-11-28T20:23:40","modified_gmt":"2015-11-28T16:23:40","slug":"rabota-s-polyami-klassa","status":"publish","type":"post","link":"https:\/\/infobase.athn.ru\/program\/2015\/11\/rabota-s-polyami-klassa\/","title":{"rendered":"\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u043e\u043b\u044f\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/questions\/9210428\/how-to-convert-class-into-dictionarystring-string\">http:\/\/stackoverflow.com\/questions\/9210428\/how-to-convert-class-into-dictionarystring-string<\/a><\/p>\n<pre>someObject.GetType()\n     .GetProperties(BindingFlags.Instance | BindingFlags.Public)\n          .ToDictionary(prop =&gt; prop.Name, prop =&gt; prop.GetValue(someObject, null))<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/4943817\/mapping-object-to-dictionary-and-vice-versa\/4944547#4944547\">http:\/\/stackoverflow.com\/questions\/4943817\/mapping-object-to-dictionary-and-vice-versa\/4944547#4944547<\/a><\/p>\n<pre>public static class ObjectExtensions\n{\n    public static T ToObject(this IDictionary source)\n        where T : class, new()\n    {\n            T someObject = new T();\n            Type someObjectType = someObject.GetType();\n\n            foreach (KeyValuePair item in source)\n            {\n                someObjectType.GetProperty(item.Key).SetValue(someObject, item.Value, null);\n            }\n\n            return someObject;\n    }\n\n    public static IDictionary AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)\n    {\n        return source.GetType().GetProperties(bindingAttr).ToDictionary\n        (\n            propInfo =&gt; propInfo.Name,\n            propInfo =&gt; propInfo.GetValue(source, null)\n        );\n\n    }\n}\n\nclass A\n{\n    public string Prop1\n    {\n        get;\n        set;\n    }\n\n    public int Prop2\n    {\n        get;\n        set;\n    }\n}\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        Dictionary dictionary = new Dictionary();\n        dictionary.Add(\"Prop1\", \"hello world!\");\n        dictionary.Add(\"Prop2\", 3893);\n        A someObject = dictionary.ToObject<A>();\n\n        IDictionary objectBackToDictionary = someObject.AsDictionary();\n    }\n}<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/2051834\/exclude-property-from-gettype-getproperties\">http:\/\/stackoverflow.com\/questions\/2051834\/exclude-property-from-gettype-getproperties<\/a><\/p>\n<pre>public class SkipPropertyAttribute : Attribute\n{\n}\n\npublic static class TypeExtensions\n{\n    public static PropertyInfo[] GetFilteredProperties(this Type type)\n    {\n        return type.GetProperties().Where(pi =&gt; pi.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length == 0).ToArray();\n    }       \n}\n\npublic class Test\n{\n    public string One { get; set; }\n\n    [SkipProperty]\n    public string Two { get; set; }\n}\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        var t = new Test();\n        Type ty = t.GetType();\n\n        PropertyInfo[] pinfo = ty.GetFilteredProperties();\n        foreach (PropertyInfo p in pinfo)\n        {\n            Console.WriteLine(p.Name);\n        }\n\n        Console.ReadKey();\n    }\n}<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/4951233\/compare-two-objects-and-find-the-differences\">http:\/\/stackoverflow.com\/questions\/4951233\/compare-two-objects-and-find-the-differences<\/a><\/p>\n<pre>\nstatic class extentions\n{\n    public static List DetailedCompare(this T val1, T val2)\n    {\n        List variances = new List();\n        FieldInfo[] fi = val1.GetType().GetFields();\n        foreach (FieldInfo f in fi)\n        {\n            Variance v = new Variance();\n            v.Prop = f.Name;\n            v.valA = f.GetValue(val1);\n            v.valB = f.GetValue(val2);\n            if (!v.valA.Equals(v.valB))\n                variances.Add(v);\n\n        }\n        return variances;\n    }\n\n\n}\nclass Variance\n{\n    public string Prop { get; set; }\n    public object valA { get; set; }\n    public object valB { get; set; }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/stackoverflow.com\/questions\/9210428\/how-to-convert-class-into-dictionarystring-string someObject.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(prop =&gt; prop.Name, prop =&gt; prop.GetValue(someObject, null)) http:\/\/stackoverflow.com\/questions\/4943817\/mapping-object-to-dictionary-and-vice-versa\/4944547#4944547 public static class ObjectExtensions { public static T ToObject(this IDictionary source) where T : class, new() { <a class=\"more-link\" href=\"https:\/\/infobase.athn.ru\/program\/2015\/11\/rabota-s-polyami-klassa\/\">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":[255,295],"class_list":["post-284","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-polya","tag-tipyi"],"_links":{"self":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/284","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=284"}],"version-history":[{"count":0,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"wp:attachment":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}