{"id":116,"date":"2016-04-26T01:04:47","date_gmt":"2016-04-26T01:04:47","guid":{"rendered":"http:\/\/windows.emacslisp.com\/?p=116"},"modified":"2016-05-16T02:05:36","modified_gmt":"2016-05-16T02:05:36","slug":"c-reflection-set-object-enum-property-value","status":"publish","type":"post","link":"http:\/\/windows.emacslisp.com\/index.php\/2016\/04\/26\/c-reflection-set-object-enum-property-value\/","title":{"rendered":"C# Reflection  &#8211;  set object enum property value"},"content":{"rendered":"<p>using C# Reflection can convert DataTable to Object easily.<br \/>\nHowever, it may be have some problems when converting enum which is special, we don&#8217;t know enum type and enum type should get from reflection.<\/p>\n<p>Here is code sample for c# enum property covert.  <\/p>\n<pre lang=\"csharp\" line=\"1\"> \r\nvar a =  new YourObject(); \r\nPropertyInfo propertyInfo = a.GetType().GetProperty(s);\r\nif (propertyInfo.PropertyType.IsEnum)\r\n  {\r\n    propertyInfo.SetValue(a, Convert.ChangeType(Enum.Parse(propertyInfo.PropertyType, dr[index++].ToString()), propertyInfo.PropertyType), null);\r\n  }\r\n else\r\n    propertyInfo.SetValue(a, Convert.ChangeType(dr[index++].ToString(), propertyInfo.PropertyType), null);\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using C# Reflection can convert DataTable to Object easily. However, it may be have some problems when converting enum which is special, we don&#8217;t know enum type and enum type should get from reflection. Here is code sample for c# &hellip; <a href=\"http:\/\/windows.emacslisp.com\/index.php\/2016\/04\/26\/c-reflection-set-object-enum-property-value\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/116"}],"collection":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":2,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":121,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions\/121"}],"wp:attachment":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}