{"id":4734,"date":"2014-03-30T15:01:04","date_gmt":"2014-03-30T15:01:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/generic-method-executed-with-a-runtime-type-duplicate-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:01:04","modified_gmt":"2014-03-30T15:01:04","slug":"generic-method-executed-with-a-runtime-type-duplicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/generic-method-executed-with-a-runtime-type-duplicate-collection-of-common-programming-errors\/","title":{"rendered":"Generic Method Executed with a runtime type [duplicate]-Collection of common programming errors"},"content":{"rendered":"<p>First we need to convert <code>p.Value<\/code> to the right type, since even if we know the type at compile time we can&#8217;t pass the string straight to the method&#8230;<\/p>\n<pre><code>DoSomething( \"10\" ); \/\/ Build error\n<\/code><\/pre>\n<p>For simple numeric types and DateTime, we can use<\/p>\n<pre><code>object convertedValue = Convert.ChangeType(p.Value, p.DisplayType);\n<\/code><\/pre>\n<p>Now we can use reflection to invoke the required generic method&#8230;<\/p>\n<pre><code>typeof(ClassExample)\n    .GetMethod(\"DoSomething\")\n    .MakeGenericMethod(p.DisplayType)\n    .Invoke(this, new object[] { p.Name, convertedValue });\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First we need to convert p.Value to the right type, since even if we know the type at compile time we can&#8217;t pass the string straight to the method&#8230; DoSomething( &#8220;10&#8221; ); \/\/ Build error For simple numeric types and DateTime, we can use object convertedValue = Convert.ChangeType(p.Value, p.DisplayType); Now we can use reflection to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4734","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4734"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4734\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}