admin管理员组

文章数量:1122832

I want to implement a function that accepts a general parameter and provides detailed information about the parameter, such as its type, value, and if it's an object, its properties.

For example, I would like to pass a variable like this:

var
  s: string;
begin
  s := 'Hi';
  ShowMessage(GetInfo(s)); // Expected output: s: string = 'Hi'
  ShowMessage(GetInfo(Label1.Caption)); // Expected output: (Label1: TLabel).Caption: string = 'First name:'
  ShowMessage(GetInfo(pb1.Position)); // Expected output: (pb1: TProgressBar).Position: Integer = 70
  ShowMessage(GetInfo(Edit1.Font.Color)); // Expected output: ((Edit1: TEdit).Font: TFont).Color: TColor = 16729702
end;

Is it possible to implement a function like GetInfo that understands the parameter and provides such output?

I attempted to use RTTI (Run-Time Type Information) for this, but I couldn’t achieve the expected result. Specifically, I want to retrieve the type, name, and value of the variable or property passed to the function.

Can anyone suggest how to implement this, or provide an example that would work in Delphi?

本文标签: