2016-11-21 42 views
1

添加以下代码:德尔福(XE7)泛型类通用TDictionary

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. 

我得到错误:

[dcc32 Error] Unit1.pas(35): E2010 Incompatible types: 'Unit1.TMyClass' and 'Unit1.TMyClass.T>'

符合

在那里我试图自我添加到TDictionary。我如何将泛型类添加到TDictionary中,其中第二个参数需要通用对象?

回答

3

虽然你的约束是确保T只能是TForm编译器不支持所谓的协方差。

你可以做的是hardcast SelfTMyClass<TForm>添加它。

+0

谢谢。滑稽:变量“列表”必须在界面内,而不是执行。 – Makla