C# Get Type Parameters from a Delegate which is a Func<>
<p>Say I've cast a Func&lt;> to a Delegate, for example...</p>

<pre><code>Func&lt;object, int&gt; func = GetFunc();
Delegate d = func;
</code></pre>

<p>Now, if I know the type of Delegate ahead of time, I can cast directly back to Func&lt;></p>

<pre><code>Func&lt;object, int&gt; newFunc = (Func&lt;object, int&gt;)d;
</code></pre>

<p>But what if I don't know the function type until runtime? How do I get back my Func&lt;> from a Delegate?</p>

<pre><code>Delegate d = GetFunc(); // may return a Func&lt;T&gt;, Func&lt;T,T2&gt;, Func&lt;T,T2,T3&gt;, etc...
var func = GetFuncFromDelegate(d);
</code></pre>

<p>or at least, get the Type parameters so I can rebuild the type:</p>

<pre><code>Delegate d = GetFunc(); // may return a Func&lt;T&gt;, Func&lt;T,T2&gt;, Func&lt;T,T2,T3&gt;, etc...
Type[] funcParams = GetTypeParamsFromDelegate(d);
</code></pre>