{"id":1212,"date":"2014-02-03T22:42:13","date_gmt":"2014-02-03T22:42:13","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1212"},"modified":"2014-02-03T22:42:13","modified_gmt":"2014-02-03T22:42:13","slug":"using-net-reflection-to-dynamically-create-methods-with-generic-parameters","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-net-reflection-to-dynamically-create-methods-with-generic-parameters\/","title":{"rendered":"Using .NET reflection to dynamically create methods with generic parameters"},"content":{"rendered":"<p>Following on from my post <a href=\" http:\/\/putridparrot.com\/blog\/dynamically-creating-c-code-using-the-codedomprovider\" title=\"Dynamically creating C# code using the CodeDomProvider\" target=\"_blank\">Dynamically creating C# code using the CodeDomProvider<\/a> I&#8217;ve created a new type dynamically using the CodeDomProvider and I now need to use the type within a generic method call.<\/p>\n<p>So to start with, I have a DataSerializer class (the basics are shown below &#8211; I&#8217;ve just distilled it down to the method signature that we&#8217;re interested in for this post)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class DataSerializer&lt;T&gt; where T : new()\r\n{\r\n   public static IList&lt;T&gt; Deserialize(IDelimiterSeperatedReader dsr, \r\n              string data, DelimitedDeserializeOptions options)\r\n   {\r\n      \/\/ method implementaion\r\n   }\r\n   \/\/ other methods\r\n}\r\n<\/pre>\n<p>From the previous post I have a dynamically created type (so I just have a Type object, the variable <em>generatedType<\/em> shown in the code below). I need to create a call to the static method (Deserialize) with this type as a type generic parameter.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nType generatedType = cr.CompiledAssembly.GetType(&quot;CsvGeneratedData&quot;);\r\n\r\nType&#x5B;] arguments = { typeof(IDelimiterSeperatedReader), \r\n                  typeof(string), typeof(DelimitedDeserializeOptions) };\r\n\r\nType dataserializer = typeof (DataSerializer&lt;&gt;);\r\nType typedSerializer = dataserializer.MakeGenericType(generatedType);\r\nMethodInfo mi = typedSerializer.GetMethod(&quot;Deserialize&quot;, \r\n               BindingFlags.Public | BindingFlags.Static, \r\n               null, arguments, null);\r\n<\/pre>\n<p>In the above the <em>arguments<\/em> variable is an array of Type&#8217;s in the order of the arguments the method Deserialize takes. We then use <\/p>\n<p>This code creates the MethodInfo object but we now need to invoke the method itself, so we use the following <em>Type dataserializer = typeof (DataSerializer&lt;&gt;)<\/em> to create a DataSerializer but at this point it does not have the generic parameter assigned to it, so we then use <em>MakeGenericType<\/em> to assign the generic parameter to the <em>dataserializer<\/em>. Finally we get the method info. for the static method we want to call.<\/p>\n<p>Now we need to invoke the method using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nmi.Invoke(null, parameters);\r\n<\/pre>\n<p>where parameters is an object array with the relevant method arguments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following on from my post Dynamically creating C# code using the CodeDomProvider I&#8217;ve created a new type dynamically using the CodeDomProvider and I now need to use the type within a generic method call. So to start with, I have a DataSerializer class (the basics are shown below &#8211; I&#8217;ve just distilled it down to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1212","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1212"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1212\/revisions"}],"predecessor-version":[{"id":1223,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1212\/revisions\/1223"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}