procedure GetClassProperties(AClass: TObject
AStrings: TStrings);
var
PropList: PPropList;
ClassTypeInfo: PTypeInfo;
enumdata,ClassTypeData: PTypeData;
i,j,NumProps: integer;
tmpstr:string;
tmpobj:Tobject;
begin
ClassTypeInfo := AClass.ClassInfo;
ClassTypeData := GetTypeData(ClassTypeInfo);
if ClassTypeData.PropCount <> 0 then
begin
GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
try
GetPropInfos(AClass.ClassInfo, PropList);
for i := 0 to ClassTypeData.PropCount - 1 do
begin
tmpstr:='';
case PropList^.PropType^.Kind of
tkMethod:continue;
tkInteger,tkInt64:tmpstr:=inttostr(GetOrdProp(AClass,PropList^.Name));
tkEnumeration:begin
tmpstr:=GetEnumProp(AClass,PropList^.Name)+' {';
enumdata:=GetTypeData(PropList^.PropType^);
//下面两行可以进行非常大的优化
for j:=enumdata.MinValue to enumdata.MaxValue do
tmpstr:=tmpstr+GetEnumName(PropList^.PropType^,j)+',';
tmpstr[length(tmpstr)]:='}'
end;
tkFloat:tmpstr:=floattostr(GetFloatProp(AClass,PropList^.Name));
tkClass:begin
tmpobj:=GetObjectProp(AClass,PropList^.Name);
if (tmpobj<>nil) and (tmpobj is Tcomponent) then
tmpstr:=Tcomponent(tmpobj).Name;
end;
tkString,tkLString,tkWString:tmpstr:=#39+GetStrProp(AClass,PropList^.Name)+#39;
end;{case}
if tmpstr<>'' then
tmpstr:='='+tmpstr;
AStrings.Add(Format('%s: %s%s', [PropList^.Name, PropList^.PropType^.Name,tmpstr]));
end;
NumProps := GetPropList(AClass.ClassInfo, [tkMethod], PropList);
if NumProps <> 0 then
AStrings.Add('=========EVENTS================');
for i := 0 to NumProps - 1 do
begin
tmpstr:='';
if GetMethodProp(Aclass,PropList^.Name).data<>nil then
tmpstr:=' (have events)';
AStrings.Add(Format('%s: %s%s', [PropList^.Name, PropList^.PropType^.Name,tmpstr]));
end;
finally
FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
end;
end;
end;
上面是我的程序,你自己看看核心,怎么处理集合的。
enumdata:=GetTypeData(PropList^.PropType^);
//下面两行可以进行非常大的优化
for j:=enumdata.MinValue to enumdata.MaxValue do
tmpstr:=tmpstr+GetEnumName(PropList^.PropType^,j)+',';
tmpstr[length(tmpstr)]:='}'
下面是调用函数
var
tmpform:Tform;
tmpmemo:Tmemo;
begin
tmpform:=Tform.Create(application);
tmpform.Width:=550;
tmpform.Height:=400;
tmpform.Font:=Font;
tmpform.Position:=PoDesktopCenter;
tmpmemo:=Tmemo.Create(tmpform);
tmpmemo.Parent:=tmpform;
tmpmemo.Align:=alclient;
tmpmemo.Font.Color:=clblue;
tmpmemo.ScrollBars:=ssVertical;
GetClassProperties(Tform1(self),tmpmemo.Lines);
tmpform.ShowModal;
tmpmemo.Free;
tmpform.Free;
end;