Delphi 系统[29]关键字和保留字 dispinterface、dispid

Delphi 系统[29]关键字和保留字  dispinterface、dispid 

1、定义:

  • dispinterface :用于声明一个特定的适配器接口, 这个适配器能够接受标准系统接口中传入传出的数据。用 DispInterface 声明的接口不能被继承,只能够被引用。DispInterface 方法只能调用,并且必须被动态绑定。可以通过 DispId 为接口内方法分配适配序号。DispInterface 仅能用于 Windows 平台, 如果在 Linux下进行开发, 则此关键字会自动被系统屏蔽。
  • dispid :DispInterface 接口中,用于指定特定的适配序号。在 DispInterface接口中, 适配序号必须是唯一的。如果不指定 DispId,则系统会自动分配适配序号给接口内每一个方法,可以通过适配序号访问 DispInterface 接口中的方法。

2、示例:

type
  IStringsDisp = dispinterface
    ['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}']
    property ControlDefault[Index: Integer]: Olevariant dispid 0; default;
    function Count: Integer; dispid 1;
    property Item[Index: Integer]: Olevariant dispid 2;
    procedure Remove(Index: Integer); dispid 3;
    procedure Clear; dispid 4;
    function Add(Item: Olevariant): Integer; dispid 5;
    function _NewEnum: IUnknown; dispid - 4;
  end;

3、关于 dispinterface 和 dispid  的一些说明:

分派接口类型定义自动化对象通过IDispatch实现的方法和属性。对分派接口方法的调用在运行时通过IDispatch的Invoke方法进行路由;类不能实现分派接口。

分派接口类型声明的形式为:

type interfaceName = dispinterface
 ['{GUID}']
memberList
end;

其中['{GUID}']是可选的,memberList由属性和方法声明组成。分派接口声明类似于常规接口声明,但它们不能指定祖先。例如:  

type
  IStringsDisp = dispinterface
    ['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}']
    property ControlDefault[Index: Integer]: OleVariant dispid 0; default;
    function Count: Integer; dispid 1;
    property Item[Index: Integer]: OleVariant dispid 2;
    procedure Remove(Index: Integer); dispid 3;
    procedure Clear; dispid 4;
    function Add(Item: OleVariant): Integer; dispid 5;

    function _NewEnum: IUnknown; dispid -4;
  end;

3.1 分派接口方法(仅限Windows)

分派接口的方法是对底层IDispatch实现的Invoke方法进行调用的原型。要为方法指定自动化分派ID,请在其声明中包含dispid指令,后跟整数常量;指定已使用的ID会导致错误。

分派接口中声明的方法不能包含dispid以外的指令。参数和结果类型必须是自动的——也就是说,它们必须是Byte、Currency、Real、Double、Longint、Integer、Single、Smallint、AnsiString、WideString、TDateTime、Variant、OleVariant、WordBool或任何接口类型。

3.2 分派接口属性

分派接口的属性不包括访问说明符。它们可以声明为只读或只读。要为属性指定分派ID,请在其声明中包含dispid指令,后跟整数常量;指定已使用的ID会导致错误。数组属性可以声明为默认值。分派接口属性声明中不允许使用其他指令。

  

  

创建时间:2021.08.16  更新时间:

博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
原文地址:https://www.cnblogs.com/guorongtao/p/15148351.html