title: When Does Method Resolution/Lookup Occur During Runtime?
body: <p>I have the following code to add debugging information to a message when in Debug mode:</p>
  
  <p><strong>Code In Tester DLL</strong></p>
  
  <pre><code>private string GetMessage(Foo foo)
  {
      var messageBuilder = new StringBuilder();
  
      // add message info
  
  #if DEBUG
      messageBuilder.AppendLine(foo.GetDebuggingInfo());
  #endif
  
      return messageBuilder.ToString();
  }
  </code></pre>
  
  <p><strong>Library Under Test</strong></p>
  
  <p>C++ library called from C# via a Managed C++ Wrapper project</p>
  
  <pre><code>#ifdef DEBUG
  string Foo::GetDebuggingInfo()
  {
      ...
  }
  #endif
  </code></pre>
  
  <p>Now the issue I ran into is <code>Foo</code> is declared in a separate library, and <code>GetDebuggingInfo</code> is also conditionally compiled in.  And for backward compatibility reasons the tester DLL is ran against all previously released versions of the library under test to make sure the output our server sends will continue to be ingested as expected across all differently versioned clients.  The test code is then copied out to a folder, along with the desired version of the library to test, not the version the tester was compiled against.  Everything runs fine until my code reaches <code>GetMessage</code> and that's when I encounter a <code>MethodNotFoundException</code>.</p>
  
  <p>My first attempt to resolve this, I thought of using reflection to make sure the function was present before calling the function, like so:</p>
  
  <pre><code>#if DEBUG
      if (foo.GetType().GetMethod("GetDebuggingInfo", BindingFlags.Instance | BindingFlags.Public) != null)
          messageBuilder.AppendLine(foo.GetDebuggingInfo());
  #endif
  </code></pre>
  
  <p>I figured that would keep the method from being called even when the Tester DLL is a Debug version and the DLL under Test was a release version.  Still same problem.  After adding some additional trace debugging statements, I discovered that <code>GetMessage</code> wasn't even being entered.  Upon realizing this, I tried moving the problematic method call into it's own function, and everything worked.</p>
  
  <pre><code>#if DEBUG
      // reflection check remains to keep the call from blowing
      // up when Tester is DEBUG and Under Test is RELEASE
      if (foo.GetType().GetMethod("GetDebuggingInfo", BindingFlags.Instance | BindingFlags.Public) != null)
          AddDebuggingInfo(messageBuilder, foo);
  #endif
  
  #if DEBUG
  private void AddDebuggingInfo(StringBuilder messageBuilder, Foo foo) =&gt;
      messageBuilder.AppendLine(foo.GetDebuggingInfo());
  #endif
  </code></pre>
  
  <p>So even though I have a working solution for my needs, I don't understand how my final solution worked, when I figured the reflection check would have been sufficient to keep the code from throwing a <code>MethodNotFoundException</code>.  So at runtime, when are methods looked up and resolved?</p>