Mar*_*ner 14 delphi generics deep-copy tdictionary delphi-xe2
有一种方法或简单方法如何将一个TDictionary内容复制到另一个中?假设我有以下声明
type
TItemKey = record
ItemID: Integer;
ItemType: Integer;
end;
TItemData = record
Name: string;
Surname: string;
end;
TItems = TDictionary<TItemKey, TItemData>;
var
// the Source and Target have the same types
Source, Target: TItems;
begin
// I can't find the way how to copy source to target
end;
Run Code Online (Sandbox Code Playgroud)
我想将源1:1复制到目标.有这样的方法吗?
谢谢!
Jer*_*non 24
TDictionary有一个构造函数,允许您传入另一个集合对象,该对象将通过复制原始内容来创建新对象.那是你在找什么?
constructor Create(Collection: TEnumerable<TPair<TKey,TValue>>); overload;
Run Code Online (Sandbox Code Playgroud)
所以你会用
Target := TItems.Create(Source);
Run Code Online (Sandbox Code Playgroud)
而Target将被创建为Source的副本(或至少包含Source中的所有项目).