为什么可以在 Office Interop 中创建接口实例?

发布于 2024-07-07 21:25:42 字数 253 浏览 5 评论 0原文

我在使用 Office Interop 类时多次看到这种情况。

this.CustomXMLParts.Add(MyResources.Data, new Office.CustomXMLSchemaCollection());

如果我将鼠标悬停在 CustomXMLSchemaCollection 类上,它会显示为一个界面。 那我怎么可以做一个新的呢? 是什么赋予了? 顺便说一句,这段代码可以编译并运行。

I've seen this quite a few times while using Office Interop classes

this.CustomXMLParts.Add(MyResources.Data, new Office.CustomXMLSchemaCollection());

If I hover over the CustomXMLSchemaCollection class, it shows up as an interface. Then how come I can do a new on it ? What gives?
BTW this code compiles and works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-07-14 21:25:42

您不是在创建 CustomXMLSchemaCollection 接口的实例,而是创建 CustomXMLSchemaCollectionClass coclass 的实例。

CustomXMLSchemaCollection 接口的定义是:

[Guid("000CDB02-0000-0000-C000-000000000046")]
[CoClass(typeof(CustomXMLSchemaCollectionClass))]
public interface CustomXMLSchemaCollection : _CustomXMLSchemaCollection
{
}

这意味着实现该接口的指定组件类是 CustomXMLSchemaCollectionClass。 我的猜测是,当 C# 编译器看到新的 CustomXMLSchemaCollection 接口时,它会根据该接口提供的属性将其转换为创建 CustomXMLSchemaCollectionClass 的 COM 实例。

写完这个简单的例子后:

namespace ConsoleApplication2
{
    using System;
    using Office = Microsoft.Office.Core;

    class Program
    {
        static void Main(string[] args)
        {
            Office.CustomXMLSchemaCollection test = new Office.CustomXMLSchemaCollection();
        }
    }
}

我刚刚运行了 ildasm 并获得以下 MSIL:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       8 (0x8)
  .maxstack  1
  .locals init ([0] class [Interop.Microsoft.Office.Core]Microsoft.Office.Core.CustomXMLSchemaCollection test)
  IL_0000:  nop
  IL_0001:  newobj     instance void [Interop.Microsoft.Office.Core]Microsoft.Office.Core.CustomXMLSchemaCollectionClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ret
} // end of method Program::Main

如您所见,构造的类是 CustomXMLSchemaCollectionClass 以证明我最初的假设。

You are not creating an instance of the CustomXMLSchemaCollection interface but an instance of the CustomXMLSchemaCollectionClass coclass.

The definition for CustomXMLSchemaCollection interface is:

[Guid("000CDB02-0000-0000-C000-000000000046")]
[CoClass(typeof(CustomXMLSchemaCollectionClass))]
public interface CustomXMLSchemaCollection : _CustomXMLSchemaCollection
{
}

This means that the designated coclass that implements the interface is CustomXMLSchemaCollectionClass. My guess is that when the C# compiler sees the new for CustomXMLSchemaCollection interface it translates it to create a COM instance of the CustomXMLSchemaCollectionClass based on the attributes provided with the interface.

After writing this simple example:

namespace ConsoleApplication2
{
    using System;
    using Office = Microsoft.Office.Core;

    class Program
    {
        static void Main(string[] args)
        {
            Office.CustomXMLSchemaCollection test = new Office.CustomXMLSchemaCollection();
        }
    }
}

I just ran ildasm and get the following MSIL:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       8 (0x8)
  .maxstack  1
  .locals init ([0] class [Interop.Microsoft.Office.Core]Microsoft.Office.Core.CustomXMLSchemaCollection test)
  IL_0000:  nop
  IL_0001:  newobj     instance void [Interop.Microsoft.Office.Core]Microsoft.Office.Core.CustomXMLSchemaCollectionClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ret
} // end of method Program::Main

As you can see the class that is constructed is CustomXMLSchemaCollectionClass to prove my initial assumption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文