title: WebBrowserSite: how to call a private COM interface method in a derived class?
body: <p>Here is the challenge. I'm deriving from the Framework's <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.webbrowsersite%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>WebBrowserSite</code></a> class. An instance of my derived class, <code>ImprovedWebBrowserSite</code>, is returned via <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createwebbrowsersitebase%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>WebBrowser.CreateWebBrowserSiteBase</code></a>, which I override in my derived version of the <a href="http://msdn.microsoft.com/en-us/library/System.Windows.Forms.WebBrowser%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>WebBrowser</code></a> class - specifically to provide a custom site object. The Framework's <code>WebBrowser</code> implementation further passes it to down to the underlying unmanaged WebBrowser ActiveX control.</p>
  
  <p>So far, I've managed to override <code>IDocHostUIHandler</code> in my <code>ImprovedWebBrowserSite</code> implementation (like <a href="https://stackoverflow.com/a/19684075/1768303">this</a>). I'm now looking for more core COM interfaces, like <code>IOleClientSite</code>, which I want to pass-through to <code>WebBrowserSite</code>. All of them are exposed to COM with <code>ComImport</code>, but declared as <code>private</code> or <code>internal</code> by the Framework's implementation of <code>WebBrowserSite</code>/<code>UnsafeNativeMethods</code>. Thus, I cannot <a href="http://msdn.microsoft.com/en-us/library/aa664593%28v=vs.71%29.aspx" rel="nofollow noreferrer">explicitly re-implement</a> them in the derived class. I have to define my own versions, like I did with <code>IDocHostUIHandler</code>.</p>
  
  <p><strong>So, the question is, how do I call a method of a private or internal COM interface defined in <code>WebBrowserSite</code>, from my derived class?</strong> For example, I want to call <code>IOleClientSite.GetContainer</code>. I can use reflection (like <a href="https://stackoverflow.com/a/1216848/1768303">this</a>), but that would be the last resort, second to re-implementing <code>WebBrowser</code> from scratch.</p>
  
  <p>My thinking is, because the Framework's private <code>UnsafeNativeMethods.IOleClientSite</code> and my own <code>ImprovedWebBrowserSite.IOleClientSite</code> are both <em>COM</em> interfaces, declared with the <code>ComImport</code> attribute, the same GUID and identical method signatures. There's <a href="http://msdn.microsoft.com/en-us/library/dd997297.aspx" rel="nofollow noreferrer">COM Type Equivalence</a> in .NET 4.0+, so there has to be a way to do it without reflection.</p>
  
  <p><strong>[UPDATE]</strong> Now that I've got a <a href="https://stackoverflow.com/a/19739699/1768303">solution</a>, I believe it opens some new and interesting possibilities in customizing the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx" rel="nofollow noreferrer">WinForms version</a> of <code>WebBrowser</code> control.</p>
  
  <p>This version of the question was created after <a href="https://stackoverflow.com/q/19712462/1768303">my initial attempt</a> to formulate the problem in a more abstract form was dubbed misleading by a commentator. The comment has been removed later, but I decided to keep both versions.<br></p>
  
  <p><strong>Why did I not want to use reflection to solve this problem?</strong> For a few reasons:</p>
  
  <ul>
  <li><p>Dependency on the actual symbolic names of the internal or private methods, as given by the implementers of <code>WebBrowserSite</code>, unlike with a COM interface, which is about the binary v-table contract.</p></li>
  <li><p>Bulky reflection code. E.g., consider calling the base's private <a href="http://msdn.microsoft.com/en-us/library/aa753266(v=vs.85).aspx" rel="nofollow noreferrer"><code>TranslateAccelerator</code></a> via <code>Type.InvokeMember</code>, and I have ~20 methods like that to call.</p></li>
  <li><p>Although less important, efficiency: a late-bound call via reflection is always less efficient than a direct call to a COM interface method via v-table.</p></li>
  </ul>