如何在字典中存储类型?

时间:2015-12-27 11:34:10

标签: delphi generics

我想在Delphi中创建一个类型的字典。

如果我宣布以下内容:

m_things: TDictionary<TClass, TThing>;

它为我提供了针对TThing实例的任何类型的字典。如何将类型限制为TThing或派生类的实例?我想这样做:

m_things: TDictionary<class of TThing, TThing>;

但是我收到以下错误:

[dcc32 Error] collector.pas(13): E2058 Class, interface and object types only allowed in type section

我也尝试过:

m_abstract: TDictionary<T: TThing, TThing>;

然后我收到了这个错误:

[dcc32 Error] collector.pas(13): E2003 Undeclared identifier: 'T'

我不清楚这是否可能,以及语法可能是什么。

1 个答案:

答案 0 :(得分:3)

您需要使用class of语法声明一种类型来表示metaclass。像这样:

type
  TThingClass = class of TThing;
....
var
  m_things: TDictionary<TThingClass, TThing>;
相关问题