uses System.Rtti,
System.Generics.Collections;
type
TCountry = class
private
FInfo: TDictionary<string, TValue>;
public
constructor Create;
procedure Add(const Key: string;
const Value: TValue);
//properties cannot be generic:
//a generic method instead
function Get<T>(const Key: string): T;
end;
constructor TCountry.Create;
begin
FInfo := TDictionary<string, TValue>.Create;
end;
procedure TCountry.Add(const Key: string;
const Value: TValue);
begin
FInfo.AddOrSetValue(Key, Value);
end;
function TCountry.Get<T>(
const Key: string): T;
begin
Result := FInfo[Key].AsType<T>;
end;
var
France: TCountry;
begin
France := TCountry.Create;
France.Add('Name', 'France');
France.Add('Population', 66991000);
WriteLn('name is ',
France.Get<string>('Name'));
//name is France
WriteLn('population is ',
France.Get<Integer>('Population'));
//population is 66991000
end.