Why does accessing a COM object from .NET, without going through the Interop class, sometimes work?

后端 未结 3 2260
面向向阳花
面向向阳花 2021-02-19 23:06

When you interface a COM object from .NET code, VS creates an interop DLL, with interop classes.

Example:

You have a foo.dll the implements a COM library Foo, th

3条回答
  •  北海茫月
    2021-02-19 23:41

    Great question. Many people are surprised this works at all because Bar is an interface and surely you shouldn't be able to create a new instance of an interface! But although I can't seem to find any specifics of the implementation, I remember reading in Adam Nathan's COM interop book that C# makes a special exception for COM interfaces marked with a CoClassAttribute and turns the call into an instantiation of the coclass instead.

    But I don't know why it would sometimes work and sometimes not work.

提交回复
热议问题