TValue
表示编译器支持的相同隐式强制转换。例如,Int16<->Integer
,但不是 String<->Integer
。那些善意的转换,你必须自己做
我可能会这样做:
type
ConvFunc = function(const Value: String): TValue;
function ConvToInt(const Value: String): TValue;
begin
Result := StrToInt(Value);
end;
// other conversion functions as needed...
var
Dict: TDictionary<String,ConvFunc>;
Func: ConvFunc;
Value: TValue;
begin
Dict := TDictionary<String,ConvFunc>.Create;
Dict.Add('name',@ConvToInt);
// add other entries as needed...
...
if not Dict.TryGetValue('NameToFind',Func) then
begin
// name is not supported...
end else
begin
try
Value := Func('InputToConvert');
// use Value as needed...
except
// input is not supported...
end;
end;
...
Dict.Free;
end;
本文链接:https://www.f2er.com/1014543.html