with code like the following (sorry about the rubbish names..its just my play area)
procedure TForm3.WebButton1Click(Sender: TObject);
var
c: TRttiContext;
i: integer;
cc: TControl;
t: TRttiType;
p: TRttiProperty;
s: string;
a: TCustomAttribute;
f: TRttiField;
begin
c := TRttiContext.Create;
for i:=0 to ControlCount-1 do
begin
cc := Controls[i];
t := c.GetType(cc.ClassType);
s := t.Name;
Memo.Lines.Add(s);
for f in t.GetDeclaredFields do
begin
memo.Lines.Add(' ' + f.Name);
for a in f.GetAttributes do
begin
Memo.Lines.Add('>>>>>>> ' + a.ClassName);
end;
end;
end;
end;
I'm unable to get the field names displayed. In XE, I'd use GetFields but in WEB CORE's RTTI unit, it has GetDeclaredFields instead but that seems to always return a nil.
as an interim solution, I can get what I require form this code I wrote as a proof of concept.
uses
TypInfo;
procedure TForm3.Test;
var
t: TTypeInfoStruct;
a: TTypeMemberPropertyDynArray;
aa: TTypeInfoAttributes;
atr: TCustomAttributeArray;
p: TTypeMemberProperty;
i,j: integer;
f: TTypeMemberField;
c: TCustomAttribute;
begin
t := TypeInfo(TForm3);
memo.lines.add(inttostr(t.fieldcount));
for i:=0 to t.FieldCount-1 do
begin
f := t.GetField(i);
Memo.Lines.Add(f.TypeInfo.Name);
aa := t.GetField(i).Attributes;
for c in GetRTTIAttributes(aa) do
begin
memo.lines.add(c.ClassName + ' -> ' + (c as TSampleAttribute).MyS);
end;
end;
memo.lines.add('------------------------------');
a := GetPropList(t,[tkClass], false);
for p in a do
begin
memo.Lines.Add(p.TypeInfo.Name);
end;
end;
its ugly but might be handy as a reference for anyone else trying a similar operation.
What I have found is that there are the GetXXXProp functions to get properties, but theres not GetXXXField functions to get fields.
In a TForm or TFrame when you drop components onto the form, they appear as fields in the published section. There doesnt seem to be any way to use RTTI to get this information (eg GetObjectField(Instance: TObject; FieldName: String): TObject; would be perfect)
I'll likely have to see if I can get what I need from the Controls list on the form/frame.
It looks like Delphi can get values from fields, but the RTTI unit for Web Core has that method commented out. Any idea if that will be available when 1.6 is officially released ?
1.6 was released Jan 13, 2021.
For the RTTI related questions part of the pas2js RTL, we will need to check this with the pas2js team.