我新的仿製藥,並需要一些幫助,以結構類和實現方法 - 德爾福2010結構德爾福泛型
我想使用泛型系列化任何TObject的JSON ...我想成爲能夠重用代碼。
我的問題如下:
如何創建一個泛型構造函數?我希望能夠使用Self或Default(T),但它只返回nil。
五:= Marshal.Marshal(返回object)...這種方法需要一個TObject的,但我不知道如何引用中傳遞了當前對象
另外,我該如何使用。這裏面的方法 - 見下面Q3
任何其他意見非常讚賞。
/////////////////////// ////////////////////
TFileOperationResult = class(TObject)
private
FSuccess: Boolean;
//Error: PException;
FLastError: Integer;
function GetFailure: Boolean;
property Failure: Boolean read GetFailure;
public
property Success: Boolean read FSuccess write FSuccess;
property LastError: Integer read FLastError write FLastError;
end;
TResponseObject<T: class> = class(TObject)
private
FReturnObject: T;
function GetReturnObject: T;
function BaseStringsConverter(Data: TObject): TListOfStrings;
public
constructor Create; overload;
property ReturnObject: T read GetReturnObject;
procedure Serialize;
end;
constructor TResponseObject<T>.Create;
begin
// Question 1 - What should go in here?
end;
function TResponseObject<T>.GetReturnObject: T;
begin
Result := Default(T);// Is this correct?
end;
procedure TResponseObject<T>.Serialize;
var
Marshal: TJSONMarshal;
V: TJSONValue;
begin
Marshal := TJSONMarshal.Create(TJSONConverter.Create);
Marshal.RegisterConverter(TResponseObject<T>, BaseStringsConverter);
V := Marshal.Marshal(ReturnObject); // Question 2 - How Can I refer to 'Self'?
OutPut := V.ToString;
Marshal.Free;
end;
/////////////////////// ////////////////////////////////////////
procedure TForm1.Test;
var
FileOperationResult: TResponseObject<TFileOperationResult>;
begin
FileOperationResult := TResponseObject<TFileOperationResult>.Create;
FileOperationResult.Serialize;
end;
//問題3
procedure TForm1.MoveCopyFile<THowNowResponse>(ASource, DDestination: String);
var
FileOperationResult: TFileOperationResult;
begin
FileOperationResult := TFileOperationResult.Create;
// What to do?
end;
真的很難知道你想要什麼......我認爲如果你寫出不同的問題會更好,因爲你似乎有不同的問題,並試圖在這裏解決它們。對於泛型部分... JSON Marshaler能夠編組任何類,所以我不確定是否要創建一個泛型類來使用它作爲封裝來封送任何僅使用默認TJsonConverter的對象,或者什麼(可能TResponseObject類的名稱使我不清楚)。 – jachguate 2011-01-11 17:44:05