delphi - 将具有通用参数的类传递给Delphi中的另一个类

标签 delphi generics

我在Delphi XE5中有2个类,并将一个传递给另一个:

  TfrmBaseList = class(TForm)
  private
    FListOwner: TSystemBaseList<TSystemColumnEntity>;
  public
    constructor Create(AListOwner: TSystemBaseList<TSystemColumnEntity>); virtual;
  end

  TSystemBaseList<T: TSystemColumnEntity> = class(TPersistent)
  public
    procedure Execute;
    property SelectedValues: TObjectList<T> read 
  end;


  procedure TSystemBaseList<T>.Execute;
  var
    frmList: TfrmBaseList;
  begin
   //frmList := TfrmBaseList.Create(Self<T>)
   //frmList := TfrmBaseList.Create(Self<TSystemColumnEntity>)  
   frmList := TfrmBaseList.Create(???????)
  end;


如何将TSystemBaseList传递给TfrmBaseList类的构造方法?

此构造函数仅创建一个Form,然后将AListOwner分配给FListOwner,
我可以将此构造函数更改为以下属性:

TfrmBaseList = class(TForm)
private
  FListOwner: TSystemBaseList<TSystemColumnEntity>;
public
  property ListOwner: TSystemBaseList<TSystemColumnEntity> read FListOwner write FListOwner;
end


我该如何设置呢?

最佳答案

构造函数需要一个具体的实例,它是以下实例:

TSystemBaseList<TSystemColumnEntity>


您正在提供以下类型的未实例化通用实例:

TSystemBaseList<T>


您必须为该构造函数提供一个具体实例。在当前形式下,您无法从TSystemBaseList<T>.Execute实例化该表单。

您可能会认为,因为T必须从TSystemColumnEntity派生,所以TSystemBaseList<T>将与TSystemBaseList<TSystemColumnEntity>兼容。但这不是事实,因为不支持任何通用差异。在此处阅读有关此主题的更多信息:Generics and variance

一种前进的方法是使表单类型也通用。尽管这在IDE表单设计器中不能很好地发挥作用。我怀疑需要更彻底的重新设计来解决您的问题。我不提供有关重新设计的建议,因为我不知道问题所在。

关于delphi - 将具有通用参数的类传递给Delphi中的另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31624343/

相关文章:

德尔福:F2084 内部错误 T2575

java - 创建类和接口(interface)的数组列表

delphi - 如何捕获突然破坏的TThread

delphi - 方法指针和常规过程不兼容

c# - 系统.InvalidCastException : Unable to cast object of type x to type y

swift - 从正常集合索引和反向集合索引创建范围

c# - 在没有类约束的情况下将一个泛型转换为另一个泛型

c# - 排除通用约束中的类型(可能?)

德尔福印地 Ping 错误 10040

delphi - 跨 LAN 的安全通信