3年前的代码,今天上了一次.好久没有来.
procedure Tform1.GetClassPropertiesGrid(AClass: TObject;var AStrings: TStringGrid);
var
PropList: PPropList;
ClassTypeInfo: PTypeInfo;
ClassTypeData: PTypeData;
i,tmpint: 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);
AStrings.RowCount:=ClassTypeData.PropCount;
tmpint:=0;
for i := 0 to ClassTypeData.PropCount - 1 do
begin
tmpstr:='';
case PropList^.PropType^.Kind of
tkMethod:continue;
tkInteger,tkInt64,tkEnumeration:tmpstr:=GetEnumProp(AClass,PropList^.Name);
tkFloat:tmpstr:=floattostr(GetFloatProp(AClass,PropList^.Name));
tkString,tkLString,tkWString:tmpstr:=GetStrProp(AClass,PropList^.Name);
tkClass:begin
tmpobj:=GetObjectProp(AClass,PropList^.Name);
if (tmpobj<>nil) then
begin
if (tmpobj is Tcomponent) then
tmpstr:=Tcomponent(tmpobj).Name
else
tmpstr:='('+Tcomponent(tmpobj).ClassName+')';
end;
end;
end;
AStrings.Cells[0,i]:=PropList^.Name;
if tmpstr<>'' then
AStrings.Cells[1,i]:=tmpstr;
inc(tmpint);
end;
AStrings.RowCount:=tmpint;
finally
FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
end;
end;
end;
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;