用RTTI
https://www.jianshu.com/p/784db6186e49
uses strutils, rtti, System.typinfo; procedure TForm2.FormCreate(Sender: TObject); var context: TRttiContext; rType: TRttiType; aPropery: trttiproperty; LProperty: trttiproperty; var i: Integer; pMethod: TRttiMethod; sMethodStr : String; aParameters: System.TArray<System.rtti.TRttiParameter>; pParameter: TRttiParameter; aParaStr:string; //参数 名 字符串 aTypeStr: String; // 类型 字符串 begin context := TRttiContext.Create; ListBox1.Items.Clear; ListBox2.Items.Clear; ListBox3.Items.Clear; //===============rType := context.GetType(TListBox);//====================== rType := context.GetType(ListBox1.ClassType); //============== ListBox1.Items.Add(aPropery.ToString);===================================== for aPropery in rType.GetProperties do begin if aPropery.PropertyType.TypeKind =tkMethod then //只要 事件属性 ListBox1.Items.Add('propery '+ aPropery.Name + '+' + aPropery.PropertyType.ToString)
// + '+' + aPropery.GetValue(&ListBox1).ToString //具体值 要 指定 实例对象 // ListBox1.Items.Add(rType.AsInstance.MetaClassType.ClassName + '+' + end; //=========== ListBox2.Items.Add(pMethod.ToString);=========================== for pMethod in rType.GetMethods do begin aParameters := pMethod.GetParameters; if length( aParameters)=0 then sMethodStr := pMethod.Name else begin //没有参数就不加() sMethodStr := pMethod.Name +'('; for i := 0 to High(aParameters) do begin pParameter := aParameters[i]; aParaStr := pParameter.Name; //参数 名 字符串 aTypeStr := '';
if pParameter.ParamType <> nil then aTypeStr:= GetEnumName(TypeInfo( System.TTypeKind), ord( pParameter.ParamType.TypeKind)); //参数 类型 字符串 if i > 0 then sMethodStr := sMethodStr + '; '; sMethodStr := sMethodStr + aParaStr + ':' + aTypeStr; end; sMethodStr:=sMethodStr+')'; end; if pMethod.ReturnType <> nil then //根据返回值 确定是 func 还是 procedure sMethodStr := 'func '+ sMethodStr+': '+ GetEnumName(TypeInfo( System.TTypeKind), ord( pMethod.ReturnType.TypeKind)) else sMethodStr := 'procedure '+ sMethodStr ; ListBox2.Items.Add( // rType.AsInstance.MetaClassType.ClassName + '++' + sMethodStr); // +'++'+LMethod.MethodKind); end;
var LField: TRttiField; //=====================ListBox3.Items.Add(LField.ToString);==========================================
for LField in rType.GetFields do
ListBox3.Items.Add(
LField.Name + '+' +LField.FieldType.ToString
+ '=' +LField.GetValue(&ListBox1).ToString //具体值 要 指定 实例对象
+' @'+inttoHex(LField.Offset)
);
end;