I have:
TMyForm = class(TForm)
strict private
FMyColumn: TMyGridColumn;
...
end;
...
constructor TMyForm.Create;
begin
...
FMyColumn := TMyGridColumn;
FMyColumn.Name := 'FMyColumn';
// I wish to substirtute it with a call like this which will return 'FMyColumn'
FMyColumn.Name := GetFieldName(FMyColumn);
...
end;
I need to initialize the component (TMyGridColumn) name with some meaningful name but prefer to not hard-code its name as a string开发者_如何学Go literal.
A non-RTTI solution will be more welcome.
An object does not have any knowledge, a priori, of which fields or variables refer to it. So without more information you can't write such a function.
If your function knew which object had a reference to the column, then it could use RTTI to find its name. Without RTTI you cannot hope to do this.
I think the fundamental problem you are having is that, although you name the function GetFieldName, you are not passing a field. You are passing an object reference.
精彩评论