我新的仿制药,并需要一些帮助,以结构类和实现方法 - 德尔福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