如何在Delphi 7中将一个接口转换为另一个接口?

Big*_*ion 1 delphi delphi-7

我遇到了以下问题:

我有一个类,它是使用Delphi 7 XML Data绑定向导(New - > Other - > XML Databindng)从xsd文件生成的.

我需要找到一种方法来为生成的代码添加方法:

IXMLGlobeSettingsType = interface(IXMLNode)
    ['{9A8F5C55-F593-4C70-85D2-68FB97ABA467}']
    { Property Accessors }
    function Get_General: IXMLGeneralSettingsType;
    function Get_Projector: IXMLProjectorSettingsType;
    function Get_LineMode: IXMLLineDrawingSettingsType;

    { Methods & Properties }
    property General: IXMLGeneralSettingsType read Get_General;
    property Projector: IXMLProjectorSettingsType read Get_Projector;
    property LineMode: IXMLLineDrawingSettingsType read Get_LineMode;

    //procedure SetDefault;   {To be added}
  end;
Run Code Online (Sandbox Code Playgroud)

该接口由相应的类实现,该类也由向导生成:

TXMLGlobeSettingsType = class(TXMLNode, IXMLGlobeSettingsType)
  protected
    { IXMLGlobeSettingsType }
    function Get_General: IXMLGeneralSettingsType;
    function Get_Projector: IXMLProjectorSettingsType;
    function Get_LineMode: IXMLLineDrawingSettingsType;
  public
    procedure AfterConstruction; override;
  end;
Run Code Online (Sandbox Code Playgroud)

为了定义我自己对生成的代码的扩展,我定义了以下接口:

IDefaultable = interface
   procedure SetDefault;
end;
Run Code Online (Sandbox Code Playgroud)

使用以下实现类:

DefaultableXMLGlobeSettingsType = class(TXMLGlobeSettingsType, IDefaultable)
  public
    procedure SetDefault;
  end;
Run Code Online (Sandbox Code Playgroud)

但是,我刚刚意识到Delphi 7不允许我将一个接口转换为另一个接口(甚至从接口转换为对象).因此,以下代码将引发错误:

defaultSettings : IDefaultable;    
FGlobeSettingsIntf: IXMLGlobeSettingsType; // FGlobeSettingsIntf is in fact a DefaultableXMLGlobeSettingsType

// some code

defaultSettings := FGlobeSettingsIntf as IDefaultable; // error: operator not applicable to this operand type
Run Code Online (Sandbox Code Playgroud)

我几乎被困在这里.如何解决这个错误?在Delphi 7中是否有一种方法(甚至是一种丑陋的方法)将接口转换为对象然后再转回另一个接口.

Dav*_*nan 5

defaultSettings := FGlobeSettingsIntf as IDefaultable;
// error: operator not applicable to this operand type
Run Code Online (Sandbox Code Playgroud)

此错误表示定义IDefaultable不包含GUID.如果没有GUID,则无法查询接口,这是as操作员在此上下文中执行的操作.

当as操作员与右侧的接口一起使用时,通过调用来实现IInterface.QueryInterface.这需要GUID与接口关联.

在声明时通过添加GUID来解决问题IDefaultable.