6
이렇게 RTTI를 사용하여 변수 이름을 얻으려고합니다.RTTI를 사용하여 변수 이름 가져 오기
여기가 내 테스트입니다.
type
TStringHelper = record helper for string
function Name: string;
end;
TMyRecord = record
Field1:string;
end;
implementation
{ TStringHelper }
function TStringHelper.Name: string;
var
context : TRttiContext;
begin
context := TRttiContext.Create;
result := context.GetType(@Self).Name; // return empty
context.Free;
end;
procedure TForm2.FormCreate(Sender: TObject);
var
r : TMyRecord;
begin
ShowMessage(r.Field1.Name);
end;
반환되는 TRttiType의 이름은 비어 있습니다.
변수 이름을 가져 오는 방법이 있습니까?
설명해 주셔서 감사합니다. RTVI (런타임 변수 정보)가 있다면 좋았을 것입니다. :) –
"런타임"이 필요 없습니다. 컴파일러는 컴파일 타임에 심볼을 문자열로 바꾸는 내장 함수 만 있으면됩니다. C# 6의 nameof 함수와 같습니다. - 투표하십시오 : https://quality.embarcadero.com/browse/RSP-13290 –
투표를 사용했습니다. 감사합니다 @StefanGlienke –