加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 是否有任何泛型类型实现QueryInterface?

发布时间:2020-12-15 04:09:58 所属栏目:大数据 来源:网络整理
导读:请考虑以下代码: TMyList = class(TListIMyItem,IMyList) Delphi向我显示错误: [DCC Error] test.pas(104): E2003 Undeclared identifier: 'QueryInterface' 是否有实现IInterface的通用列表? 解决方法 Generics.Collections中的类不实现IInterface.您必
请考虑以下代码:
TMyList = class(TList<IMyItem>,IMyList)

Delphi向我显示错误:

[DCC Error] test.pas(104): E2003 Undeclared identifier: 'QueryInterface'

是否有实现IInterface的通用列表?

解决方法

Generics.Collections中的类不实现IInterface.您必须在派生类中自己介绍它并提供标准实现.或者找到一个不同的第三方容器类集合.

例如:

TInterfacedList<T> = class(TList<T>,IInterface)
protected
  FRefCount: Integer;
  function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  function _AddRef: Integer; stdcall;
  function _Release: Integer; stdcall;
end;

function TInterfacedList<T>.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID,Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TInterfacedList<T>._AddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TInterfacedList<T>._Release: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
  if Result = 0 then
    Destroy;
end;

然后,您可以声明您的专业类:

TMyList = class(TInterfacedList<IMyItem>,IMyList)

请记住,您需要像使用引用计数生命周期管理的任何其他类一样对待此类.只能通过接口引用它.

你真的想在TInterfacedList< T>之前做更多的工作.很有用.您需要声明IList< T>这会暴露列表功能.它会是这样的:

IList<T> = interface
  function Add(const Value: T): Integer;
  procedure Insert(Index: Integer; const Value: T);
  .... etc. etc.
end;

然后,您可以简单地添加IList< T>到TInterfacedList< T>支持的接口列表和基类TList< T>将履行接口合同.

TInterfacedList<T> = class(TList<T>,IInterface,IList<T>)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读