请解释为什么我能够实例化"申请表]接口在Excel VSTO [英] Please explain why I am able to instantiate the "Application" interface in Excel VSTO

查看:156
本文介绍了请解释为什么我能够实例化"申请表]接口在Excel VSTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序,它的工作原理就好了下面的C#code。它启动Excel的新实例。

I have the following C# code in my application which works just fine. It launches a new instance of Excel.

private readonly Microsoft.Office.Interop.Excel.Application _application;
_application = new Microsoft.Office.Interop.Excel.Application();
_application.Visible = true;

我最近才注意到,<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.application.aspx\">Application是一个接口类型。究竟是怎么回事,怎么可能?

I only recently noticed that Application is an interface type. What exactly is going on and how is it possible?

推荐答案

编译器允许你,如果他们使用<装饰实例化接口href=\"http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.coclassattribute.aspx\"><$c$c>CoClass属性标识实现它们具体的类(以及一<一href=\"http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comimportattribute.aspx\"><$c$c>ComImport和<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.guidattribute.aspx\"><$c$c>Guid).当你实例化的界面,你实际上是背后实例的幕后这个具体类。

The compiler allows you to instantiate interfaces if they’re decorated with a CoClass attribute identifying the concrete class that implements them (as well as a ComImport and a Guid). When you instantiate the interface, you would actually be instantiating this concrete class behind-the-scenes.

此功能是指被用作管道为COM导入类型。请注意如何展望<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.application.aspx\"><$c$c>Application接口由一个名为具体类<一支持href=\"http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.applicationclass.aspx\"><$c$c>ApplicationClass:

This "feature" is intended to be used as plumbing for COM imported types. Notice how the Outlook Application interface is backed by a concrete class named ApplicationClass:

[GuidAttribute("00063001-0000-0000-C000-000000000046")]
[CoClassAttribute(typeof(ApplicationClass))]
public interface Application : _Application, ApplicationEvents_11_Event

在大多数情况下,你应该的的申请去这些属性到你自己的界面。然而,对于示范起见,我们可以证明编译器的将会的让你利用这个优势可能在你的code实例化接口。考虑下面简单的例子(GUID是随机的):

In most circumstances, you should not go applying these attributes to your own interfaces. However, for the sake of demonstration, we can show that the compiler will allow you to take advantage of this possibility for instantiating interfaces in your code. Consider the following simple example (the GUID is random):

[ComImport]
[Guid("175EB158-B655-11E1-B477-02566188709B")]
[CoClass(typeof(Foo))]
interface IFoo
{
    string Bar();
}

class Foo : IFoo
{
    public string Bar()
    {
        return "Hello world"; 
    }
}

使用上述声明,你可以创建自己的的IFoo 接口的一个实例:

IFoo a = new IFoo();
Console.WriteLine(a.Bar());
// Output: "Hello world"

修改:尽管<一个href=\"http://stackoverflow.com/questions/11039064/please-explain-why-i-am-able-to-instantiate-the-application-interface-in-excel/11039870#comment14438702_11039870\">jonnyGold正确地指出,Excel的<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.application.aspx\"><$c$c>Application实例未装饰的的CoClass MSDN上,这似乎是一个MSDN遗漏。从的Microsoft.Office.Interop.Excel 组件的反编译的签名是:

Edit: Although jonnyGold correctly notes that the Excel Application instance is not decorated with CoClass on MSDN, this appears to be an MSDN omission. The decompiled signature from the Microsoft.Office.Interop.Excel assembly is:

[CoClass(typeof(ApplicationClass)), Guid("000208D5-0000-0000-C000-000000000046")]
[ComImport]
public interface Application : _Application, AppEvents_Event

这篇关于请解释为什么我能够实例化&QUOT;申请表]接口在Excel VSTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆