Why does calling a generic local function with a dynamic parameter produce a BadImageFormatException? : <p>Playing around with C# 7's Local Functions, I ended up with some interesting behavior. Consider the following program:</p>

<pre><code>public void Main()
{
    Console.WriteLine("Entered Main");
    DoSomething("");
}

private void DoSomething(object obj)
{
    Console.WriteLine("Entered DoSomething");
    Generic((dynamic)obj);
    GenericLocal(obj);
    GenericLocal((dynamic)obj); // This breaks the program

    void GenericLocal&lt;T&gt;(T val) =&gt; Console.WriteLine("GenericLocal");
}

private void Generic&lt;T&gt;(T val) =&gt; Console.WriteLine("Generic");
</code></pre>

<p>This produces:</p>

<blockquote>
  <p>Entered Main</p>
</blockquote>

<p>... and then throws a <code>BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)</code>. Stack trace:</p>

<pre><code>   at UserQuery.DoSomething(Object obj)
   at UserQuery.Main()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
</code></pre>

<p>(I'm running this in LINQPad, but I get similar results from dotnetfiddle.)</p>

<p>Removing the indicated line in the code yields the output you'd expect:</p>

<blockquote>
  <p>Entered Main<br>
  Entered DoSomething<br>
  Generic<br>
  GenericLocal</p>
</blockquote>

<p>Can anyone explain why?</p>
