用RTTI
uses strutils, rtti, System.typinfo; {$R *.dfm} procedure TForm2.FormCreate(Sender: TObject); var context: TRttiContext; rType: TRttiType; aPropery: trttiproperty; LProperty: trttiproperty; LField: TRttiField; var i,j: 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; ListBox4.Items.Clear; //================================================================================== rType := context.GetType(ListBox1.ClassType); //============== ListBox1.Items.Add(aPropery.ToString);===================================== for aPropery in rType.GetProperties do begin if ( LowerCase( strutils.LeftStr(aPropery.Name,2))='on') then // 假设以On开头的属性 都是 事件 Event ListBox1.Items.Add(rType.AsInstance.MetaClassType.ClassName + '++' + aPropery.Name + '++' + aPropery.PropertyType.ToString); 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; //===================ListBox3.Items.Add(LProperty.ToString);========================================= for LProperty in rType.GetProperties do begin ListBox3.Items.Add(LProperty.ToString); end; //=====================ListBox4.Items.Add(LField.ToString);========================================== for LField in rType.GetFields do begin ListBox4.Items.Add(LField.ToString); end; end;
标签:控件,rType,Items,pMethod,列表,Add,sMethodStr,ToString,属性 From: https://www.cnblogs.com/tulater/p/17924801.html