...get a string representation of an object?

Author: Thomas Stutz

Category: Misc

{
  With the ObjectBinaryToText Function you get a string
  representation of an object
  stored in a file or a stream.

  Mit ObjectBinaryToText kann die Darstellung eines in einer Datei oder
  einem Stream gespeicherten Objekts in eine verständliche Textversion
  konvertiert werden.
}

function ComponentToString(Component: TComponent): string;
var
  
BinStream: TMemoryStream;
  StrStream: TStringStream;
begin
  
BinStream := TMemoryStream.Create;
  try
    
StrStream := TStringStream.Create(Result);
    try
      
BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result := StrStream.DataString;
    finally
      
StrStream.Free;
    end;
  finally
    
BinStream.Free
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  
ShowMessage(ComponentToString(Button1));
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base