Delphi(XE7)将泛型类添加到泛型TDictionary
在以下代码中:Delphi(XE7)将泛型类添加到泛型TDictionary,delphi,generics,delphi-xe7,Delphi,Generics,Delphi Xe7,在以下代码中: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections; type TForm1 = class(TForm) private {
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
TMyClass<T: TForm> = class
public
constructor Create;
end;
var
Form1: TForm1;
List: TDictionary<integer, TMyClass<TForm>>;
implementation
{$R *.dfm}
{ TMyClass<T> }
constructor TMyClass<T>.Create;
begin
List.Add(1, self);
end;
end.
单元1;
接口
使用
Winapi.Windows、Winapi.Messages、System.SysUtils、System.Variants、System.Classes、Vcl.Graphics、,
控件、窗体、对话框、系统泛型集合;
类型
TForm1=类(TForm)
私有的
{私有声明}
公众的
{公开声明}
结束;
TMyClass=类
公众的
构造函数创建;
结束;
变量
表1:TForm1;
列表:t词典;
实施
{$R*.dfm}
{TMyClass}
构造函数TMyClass.Create;
开始
列表。添加(1,自我);
结束;
结束。
我得到一个错误:
[dcc32错误]单元1.pas(35):E2010不兼容类型:
“Unit1.TMyClass”和
'Unit1.TMyClass.T>'
在我试图将Self添加到词典的行中。如何将泛型类添加到TDictionary,其中第二个参数采用泛型对象?当您的约束确保
T
只能是TForm
时,编译器不支持所谓的协方差
您可以将
Self
硬播到TMyClass
以添加它。谢谢。有趣的是:变量“List”必须在接口内部,而不是实现内部。