相关疑难解决方法(0)

在Delphi中,如何检查IInterface引用是否实现派生但未明确支持的接口?

如果我有以下接口和一个实现它们的类 -

IBase = Interface ['{82F1F81A-A408-448B-A194-DCED9A7E4FF7}']
End;

IDerived = Interface(IBase) ['{A0313EBE-C50D-4857-B324-8C0670C8252A}']
End;

TImplementation = Class(TInterfacedObject, IDerived)
End;
Run Code Online (Sandbox Code Playgroud)

以下代码打印'Bad!' -

Procedure Test;
Var
    A : IDerived;
Begin
    A := TImplementation.Create As IDerived;
    If Supports (A, IBase) Then
        WriteLn ('Good!')
    Else
        WriteLn ('Bad!');
End;
Run Code Online (Sandbox Code Playgroud)

这有点烦人但可以理解.支持无法转换为IBase,因为IBase不在TImplementation支持的GUID列表中.可以通过将声明更改为 - 来修复

TImplementation = Class(TInterfacedObject, IDerived, IBase)
Run Code Online (Sandbox Code Playgroud)

然而,即使没有这样做,我已经知道 A实现了IBase,因为A是IDerived,而IDerived是IBase.所以,如果我遗漏支票,我可以投A,一切都会好的 -

Procedure Test;
Var
    A : IDerived;
    B : IBase;
Begin
    A := TImplementation.Create As IDerived;
    B := IBase(A);
    //Can now successfully call any of B's methods …
Run Code Online (Sandbox Code Playgroud)

delphi winapi types interface delphi-2007

12
推荐指数
2
解决办法
4504
查看次数

使用派生接口时,接口委派不起作用

在Delphi Seattle 10中,我试图使用implements关键字委托接口的实现,但是当从第一个接口派生的接口也包含在类声明中时,不接受委托.

以下代码的编译失败,并显示"缺少接口方法IInterface1.DoSomethingFor1的实现"消息.如果我从类声明中删除IInterface2,代码将编译.如果我从其他东西中导出IInterface2,那么它也会编译.

我究竟做错了什么?或者我怎样才能做到这一点?

type
  IInterface1 = interface(IInterface)
    ['{03FB3E2E-C0DD-4E17-B6CD-D333E1E7255E}']
    procedure DoSomethingFor1;
  end;

  Iinterface2 = interface(IInterface1)
    ['{89C266E2-2816-46AD-96AA-DD74E78A4D1E}']
  end;

  T1 = class(TInterfacedObject, IInterface1, IInterface2)
  private
    Fi1: IInterface1;
  public
    property I1_Delegate: IInterface1 read Fi1 implements IInterface1;
  end;
Run Code Online (Sandbox Code Playgroud)

delphi

2
推荐指数
1
解决办法
123
查看次数

标签 统计

delphi ×2

delphi-2007 ×1

interface ×1

types ×1

winapi ×1