type
TMyRecord = record
private
class constructor create;
public
class var MyField1: string;
class var MyField2: integer;
class var MyField3: extended;
class function ToString: string; static;
end;
class constructor TMyRecord.Create;
begin
TMyRecord.MyField1 := 'Hello, world!';
TMyRecord.MyField2 := 123;
TMyRecord.MyField3 := 3.1415927;
end;
class function TMyRecord.ToString: string;
var
RecType: TRTTIType;
RecFields: TArray<TRttiField>;
I: integer;
begin
RecType := TRTTIContext.Create.GetType(TypeInfo(TMyRecord));
Result := RecType.ToString;
RecFields := RecType.GetFields;
for I := 0 to High(RecFields) do
Result := Result + Format('%s: %s = %s', [RecFields[I].Name, RecFields[I].FieldType.ToString, RecFields[I].GetValue(@TMyRecord).ToString]) + sLineBreak;
end;
我试图让 TMyRecord.ToString 返回:
TMyRecord
MyField1: string = Hello, world!
MyField2: integer = 123;
MyField3: extended = 3.1415927;
但是,我在 GetValue(@TMyRecord) 上收到编译器错误 - E2029 '(' expected but ')' found
通常应使用记录的“实例”地址调用 GetValue。但在这种情况下,记录是静态的。我不想将此记录转换为普通记录、创建实例等。我正在尝试为静态记录解决此问题。如何获取应该传递给 GetValue 的地址?