TDictionary<string,T>由于某种原因,我想在周围使用包装纸。但是,当我尝试通过for编译器遍历地图时,会说:
[dcc32 Error] Unit1.pas(23): E2010 Incompatible types: 'T' and 'System.Generics.Collections.TPair<System.string,Unit1.TMyMapWrapper<T>.T>'
如何修改通用类型声明以使像这样的简单代码可编译?
这是我的简化代码:
unit Unit1;
interface
implementation
uses
Generics.Collections
;
type
TMyMapWrapper<T> = class
private
fMap : TDictionary<string,T>;
public
procedure foo;
end;
procedure TMyMapWrapper<T>.foo;
var
item : T;
begin
for item in fMap do
;
end;
end.
Run Code Online (Sandbox Code Playgroud)
如果X为类型TDictionary<A, B>,则列举的项目为类型TPair<A, B>,而不是B。
var
item: TPair<string, T>;
begin
for item in fMap do // will compile
Run Code Online (Sandbox Code Playgroud)
如果您只想枚举字典的值(类型为T),请使用
var
val: T;
begin
for val in fMap.Values do // will compile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |