我对 COM 和 coclass 没有太多背景,所以我不太明白为什么我可以使用new
具有接口的操作员。从与语言/框架无关的角度来看,为什么它能正确编译和运行是令人困惑的:
using Microsoft.Office.Interop.Excel;
public class ExcelProgram
{
static void Main(string[] args)
{
Application excel = new Application();
}
}
检验Application
在 Visual Studio 2010 中向我展示:
using System.Runtime.InteropServices;
namespace Microsoft.Office.Interop.Excel
{
// Summary:
// Represents the entire Microsoft Excel application.
[Guid("000208D5-0000-0000-C000-000000000046")]
[CoClass(typeof(ApplicationClass))]
public interface Application : _Application, AppEvents_Event
{
}
}
幕后到底发生了什么?
我相信这仅适用于 COM 接口。马克·格拉维尔有一个解释here.
简而言之,COM 接口可以与“默认”实现类配对,这样当您“实例化”接口时,您实际上是在创建该默认实现类的实例。在这种情况下Application
您的示例中的界面似乎是ApplicationClass
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)