_Application和Application有什么区别 [英] What is the difference between _Application and Application

查看:262
本文介绍了_Application和Application有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要使用Word来定义这样的变量:

Why is it that to use Word you define a variable like this:

Dim Word As Microsoft.Office.Interop.Word._Application

OR

Microsoft.Office.Interop.Word._Application Word;

然后像这样设置它:

Word = New Microsoft.Office.Interop.Word.Application

OR

Word = new Microsoft.Office.Interop.Word.Application();

应用 _Application

我怀疑一个可能是一个类,另一个可能是一个接口,或者一个可能是公共的而另一个是私有的,但这对我来说仍然没有意义.

I suspect one may be a class and the other an interface or one may be public and the other private but that still makes no sense to me.

我希望有人可以向我解释.细节越多越好.

I was hoping someone can explain it to me. The more details the better.

推荐答案

都是接口,并且Application接口是从_Application接口(以及ApplicationEvents4_Event接口一起)继承的.

Both are interfaces and the Application interface inherits from the _Application interface (together with the ApplicationEvents4_Event interface).

那么什么时候使用什么呢?区别在MSDN中进行了说明(强调):

So when to use what? The difference is explained in MSDN (emphasis added):

应用程序界面:

Application interface:

[GuidAttribute("00020970-0000-0000-C000-000000000046")]
public interface Application : _Application, 
    ApplicationEvents4_Event

这是从COM协类派生的.NET接口,托管代码要求该COM互类与相应的COM对象进行互操作. 使用此派生的接口访问COM对象的所有方法,属性和事件成员.但是,如果要使用的方法或事件在同一COM对象下共享相同的名称,则强制转换为相应的主接口以调用该方法,并强制转换为最新的事件接口以连接到该事件.有关COM对象的信息,请参考本主题.

This is a .NET interface derived from a COM coclass that is required by managed code for interoperability with the corresponding COM object. Use this derived interface to access all method, property, and event members of the COM object. However, if a method or event you want to use shares the same name under the same COM object, cast to the corresponding primary interface to call the method, and cast to the latest events interface to connect to the event. Refer to this topic for information about the COM object.

_Application接口 :

_Application interface:

[TypeLibType(4304)]
[Guid("00020970-0000-0000-C000-000000000046")]
[ComImport]
public interface _Application  { ... }

这是COM协同类中的一个主要接口,托管代码需要该接口才能与相应的COM对象进行互操作. 仅当要使用的方法与COM对象的事件具有相同的名称时才使用此主接口;在这种情况下,强制转换为此接口以调用方法,并强制转换为最新事件接口以连接到事件.否则,请使用派生自COM协类的.NET接口访问COM对象的方法,属性和事件.

This is a primary interface in a COM coclass that is required by managed code for interoperability with the corresponding COM object. Use this primary interface only when the method you want to use shares the same name as an event of the COM object; in this case, cast to this interface to call the method, and cast to the latest events interface to connect to the event. Otherwise, use the .NET interface that is derived from the COM coclass to access methods, properties, and events of the COM object.

实际后果

简而言之:除非在方法名称和事件名称之间存在歧义,否则必须在代码中使用Application而不是_Application.

In short: Use Application and not _Application in your code, unless you have to because there is an ambiguity between a method name and an event name.

例如,

Such an ambiguity exists for example between the Application.Quit event (fired when the application quits) and the Application.Quit(ref Object SaveChanges, ref Object OriginalFormat, ref Object RouteDocument) method (exits the application, when called).

要调用该方法,您可以简单地编写(例如退出而不提示保存更改):

In order to call the method, you could simply write (e.g. to quit without prompting to save changes):

Application.Quit(false);

但是,这可能会给您以下编译器警告:

However, this might give you the following compiler warning:

警告3方法"Microsoft.Office.Interop.Word._Application.Quit(引用对象,引用对象,引用对象)"与非方法"Microsoft.Office.Interop.Word.ApplicationEvents4_Event.Quit"之间的歧义.使用方法组.

Warning 3 Ambiguity between method 'Microsoft.Office.Interop.Word._Application.Quit(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.ApplicationEvents4_Event.Quit'. Using method group.

为避免警告,您可以将应用程序对象强制转换为_Application界面:

To avoid the warning, you can cast the application object to the _Application interface:

((_Application)Application).Quit(false); 

如果要预订事件,则需要将应用程序对象强制转换为适当的事件接口:

If you you want to subscribe to the event, you need to cast the application object to the appropriate event interface:

((ApplicationEvents4_Event)Application).Quit += OnApplicationQuit;

private void OnApplicationQuit()
{
    // handle event here
}

这篇关于_Application和Application有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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