簡體   English   中英

如何在字典中存儲類型?

[英]How do I store types in a dictionary?

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

我不清楚這是否可能,以及語法可能是什么。

您需要使用語法聲明一個類型來表示 class of 像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM