我正在尝试编写一个用于以JSON格式加载和存储对象的泛型类。这是我的代码:
type
TFileHandler<T:class, constructor> = class
published
class function LoadFromFile(aFileName: string):T;
class procedure SaveToFile(value: T; aFileName: string);
end;
implementation
{ TFileHandler<T> }
class function TFileHandler<T>.LoadFromFile(aFileName: string): T;
var
strings: TStringList;
begin
if aFileName = '' then begin
aFileName := TPath.Combine(TPath.GetDocumentsPath,'sample.txt');
end; {if}
if FileExists(aFileName) then begin
strings := TStringList.Create;
strings.LoadFromFile(aFileName);
result := TJSON.JsonToObject<T>(strings.Text);
strings.free();
end
else begin
result := T.Create();
end; {if}
end;
class procedure TFileHandler<T>.SaveToFile(value: T; aFileName: string);
var
strings: TStringList;
begin
if aFileName = '' then begin
aFileName := TPath.Combine(TPath.GetDocumentsPath,'sample.txt');
end; {if}
strings := TStringList.Create;
strings.Add(TJson.ObjectToJsonString(value));
strings.SaveToFile(aFileName);
strings.Free;
end;
我在另一个单位使用我的班级:
FFileHandler := TFileHandler<TUser>.Create;
procedure TMainForm.btnStoreClick(Sender: TObject);
var
aFileName: string;
strings: TStringList;
begin
FUser.Name := PersonalData.edtName.Text;
FUser.FirstName := PersonalData.edtFirstName.Text;
FUser.MobileNumber := PersonalData.edtTelephone.Text;
FUser.EMailAddress := PersonalData.edtEMail.Text;
aFileName := TPath.Combine(TPath.GetDocumentsPath,'sample.txt');
FFileHandler.SaveToFile(FUser,TPath.Combine(TPath.GetHomePath,'testuser.json'));
end;
这在32位Windows下运行良好,但是当我尝试在Android设备上使用此代码时,我甚至无法在类函数内设置断点(断点被标记为无效)。
泛型类和Android有问题吗?或者我错过了什么?
由于
卡斯帕