2015-12-27 94 views
1

我想在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' 

我不清楚如果这是可能的,什么sy ntax可能是。

回答

3

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

type 
    TThingClass = class of TThing; 
.... 
var 
    m_things: TDictionary<TThingClass, TThing>; 
+0

或许Delphi编译器可以宣布类似于通用阵列类型,'TClass = A类T' –

+0

@ Arioch'The这是一个甜蜜的想法,它将如果编译器支持它是好的。 –

+0

您可以在Quality Portal上发布该请求并让它收集投票,​​并在此处发布链接。类不是你在使用前需要定义一个类型的好东西,其他类包括'@'操作符的指针'^'和地址,对象类型(回调)的过程和过程等等。 –