title: SecurityException: ECall methods must be packaged into a system module
body: <p>I have a (C#) function similar to the following.</p>
  
  <pre><code>private static bool SpecialCase = false;
  
  public void Foo()
  {
      if (SpecialCase)
      {
          InternalMethod();
          return;
      }
  
      Console.WriteLine();
  }
  
  [MethodImpl(MethodImplOptions.InternalCall)]
  private static extern void InternalMethod();
  </code></pre>
  
  <p>When I execute this with the .NET Framework 4 inside the debugger, the method successfully prints blank line to the console and returns. When I execute it outside the debugger, it throws an exception with the following message:</p>
  
  <pre><code>System.Security.SecurityException: ECall methods must be packaged into a system module.
  </code></pre>
  
  <p>It appears the exception is thrown when the JIT compiler compiles the method as opposed to when (if) <code>InternalMethod</code> is called. Is there anything I can do (e.g. attributes) to tell the CLI to either not throw the <code>SecurityException</code>, or delay the exception until the method is actually called?</p>
  
  <p>Side note on the use case: The <code>SpecialCase</code> field is effectively false when running with Microsoft .NET Framework, and true when running under a different (specific) implementation of the CLI. When running under the Microsoft .NET Framework, the call to <code>InternalMethod</code> is effectively unreachable.</p>