Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, this is just to put a bit more flesh on Michael's answer (he's welcome to add it in if he wants to, in which case I'll remove this one).</p> <p>Looking at the original PIA for Word.Application, there are three types involved (ignoring the events):</p> <pre><code>[ComImport, TypeLibType(...), Guid("..."), DefaultMember("Name")] public interface _Application { ... } [ComImport, Guid("..."), CoClass(typeof(ApplicationClass))] public interface Application : _Application { } [ComImport, ClassInterface(...), ComSourceInterfaces("..."), Guid("..."), TypeLibType((short) 2), DefaultMember("Name")] public class ApplicationClass : _Application, Application { } </code></pre> <p>There are two interfaces for reasons that Eric Lippert talks about in <a href="https://stackoverflow.com/questions/1051464/excel-interop-worksheet-or-worksheet/1051546#1051546">another answer</a>. And there, as you said, is the <code>CoClass</code> - both in terms of the class itself and the attribute on the <code>Application</code> interface.</p> <p>Now if we use PIA linking in C# 4, <em>some</em> of this is embedded in the resulting binary... but not all of it. An application which <em>just</em> creates an instance of <code>Application</code> ends up with these types:</p> <pre><code>[ComImport, TypeIdentifier, Guid("..."), CompilerGenerated] public interface _Application [ComImport, Guid("..."), CompilerGenerated, TypeIdentifier] public interface Application : _Application </code></pre> <p>No <code>ApplicationClass</code> - presumably because that will be loaded dynamically from the <em>real</em> COM type at execution time.</p> <p>Another interesting thing is the difference in the code between the linked version and the non-linked version. If you decompile the line</p> <pre><code>Word.Application application = new Word.Application(); </code></pre> <p>in the <em>referenced</em> version it ends up as:</p> <pre><code>Application application = new ApplicationClass(); </code></pre> <p>whereas in the <em>linked</em> version it ends up as</p> <pre><code>Application application = (Application) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("..."))); </code></pre> <p>So it looks like the "real" PIA needs the <code>CoClass</code> attribute, but the linked version doesn't because there <em>isn't</em> a <code>CoClass</code> the compiler can actually reference. It has to do it dynamically.</p> <p>I might try to fake up a COM interface using this information and see if I can get the compiler to link it...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload