I would like to add some candy to the code above. If you add three published fields to the form which names and types matches the ones in the clipboard text (which are predictable when created with the Rest Debugger), you can access the imported components by these fields:
type
TForm649 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
public
published
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
end;
procedure TForm649.Button1Click(Sender: TObject);
var
binary: TMemoryStream;
cmp: TComponent;
lst: TStringList;
stream: TMemoryStream;
begin
{ clear any existing instances }
FreeAndNil(RESTClient1);
FreeAndNil(RESTRequest1);
FreeAndNil(RESTResponse1);
lst := TStringList.Create;
try
lst.Add('object myName: TComponent'); // this resembles the instance given as parameter to ReadComponent
lst.AddStrings(cArr);
lst.Add('end');
stream := TMemoryStream.Create;
try
lst.SaveToStream(stream);
stream.Position := 0;
binary := TMemoryStream.Create;
try
ObjectTextToBinary(stream, binary);
binary.Position := 0;
binary.ReadComponent(Self); // Self puts the code into the form itself, but any other component will do
finally
binary.Free;
end;
finally
stream.Free;
end;
finally
lst.Free;
end;
Memo1.Lines.Add(RESTRequest1.Params[1].Value);
end;
You can even omit the RegisterClasses call then.
The FreeAndNil of the three fields guarantees fresh components being created. It is as well possible to keep existing instances, but that can end up having properties with non default values when these are not included in the stream. (That may even be desired in some cases.)