uses TypInfo; ... var i,j :Integer; PropList :PPropList; PropInfo :PPropInfo; begin j := GetPropList(ListBox1, PropList); for i := Low(PropList^) to j-1 do begin ListBox1.Items.Add(PropList[i]^.Name+':'+VarToStr(GetPropValue(ListBox1,PropList[i]^.Name))); //取得所有的属性和当前的值 end; end;
晕... var i,j :Integer; PropList :PPropList; //PropInfo :PPropInfo; 这个是没用的..忘删了.. begin
为什么这里换了一个对象就不行了? procedure TMainForm.Button1Click(Sender: TObject); var i,j :Integer; PropList :PPropList; begin j := GetPropList(Button1{***原来是Listbox1***}, PropList); for i := Low(PropList^) to j-1 do begin ListBox1.Items.Add(PropList[i]^.Name+':'+VarToStr(GetPropValue(ListBox1,PropList[i]^.Name))); //取得所有的属性和当前的值 end; end;
我找到了一段代码,可是这段代码不能枚举对象属性的值,请各位前辈帮我修改一样,谢谢! procedure GetClassProperties(AClass: TObject; AStrings: TStrings); var PropList: PPropList; ClassTypeInfo: PTypeInfo; ClassTypeData: PTypeData; i: integer; NumProps: Integer; begin ClassTypeInfo := AClass.ClassInfo; ClassTypeData := GetTypeData(ClassTypeInfo); if ClassTypeData.PropCount <> 0 then begin // allocate the memory needed to hold the references to the TPropInfo // structures on the number of properties. GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount); //ListBox1.items:=PropList; try // fill PropList with the pointer references to the TPropInfo structures GetPropInfos(AClass.ClassInfo, PropList); for i := 0 to ClassTypeData.PropCount - 1 do // filter out properties that are events ( method pointer properties) if not (PropList[i]^.PropType^.Kind = tkMethod) then AStrings.Add (Format('%s: %s', [PropList[i]^.Name, PropList[i]^.PropType^.Name])); // Now get properties that are events (method pointer properties) NumProps := GetPropList(AClass.ClassInfo, [tkMethod], PropList); if NumProps <> 0 then begin AStrings.Add(''); AStrings.Add(' EVENTS ================ '); AStrings.Add(''); end; // Fill the AStrings with the events. for i := 0 to NumProps - 1 do AStrings.Add(Format('%s: %s', [PropList[i]^.Name, PropList[i]^.PropType^.Name])); finally FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount); end; end;end;
学习一下TypInfo单元里的内容。
...
var
i,j :Integer;
PropList :PPropList;
PropInfo :PPropInfo;
begin
j := GetPropList(ListBox1, PropList);
for i := Low(PropList^) to j-1 do
begin
ListBox1.Items.Add(PropList[i]^.Name+':'+VarToStr(GetPropValue(ListBox1,PropList[i]^.Name)));
//取得所有的属性和当前的值
end;
end;
var
i,j :Integer;
PropList :PPropList;
//PropInfo :PPropInfo; 这个是没用的..忘删了..
begin
我找到的相关函数有以下这些(但是这些都没有例子)很可惜,希望各位大师指点!
GetPropCount,GetPropDisplayString,GetProperties,GetProperty.....
我其实只想获得引用的Com对象的属性、方法、事件的全部信息!谢谢!
procedure TMainForm.Button1Click(Sender: TObject);
var
i,j :Integer;
PropList :PPropList;
begin
j := GetPropList(Button1{***原来是Listbox1***}, PropList);
for i := Low(PropList^) to j-1 do
begin
ListBox1.Items.Add(PropList[i]^.Name+':'+VarToStr(GetPropValue(ListBox1,PropList[i]^.Name)));
//取得所有的属性和当前的值
end;
end;
procedure GetClassProperties(AClass: TObject; AStrings: TStrings);
var
PropList: PPropList;
ClassTypeInfo: PTypeInfo;
ClassTypeData: PTypeData;
i: integer;
NumProps: Integer;
begin
ClassTypeInfo := AClass.ClassInfo;
ClassTypeData := GetTypeData(ClassTypeInfo);
if ClassTypeData.PropCount <> 0 then
begin
// allocate the memory needed to hold the references to the TPropInfo
// structures on the number of properties.
GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
//ListBox1.items:=PropList;
try
// fill PropList with the pointer references to the TPropInfo structures
GetPropInfos(AClass.ClassInfo, PropList);
for i := 0 to ClassTypeData.PropCount - 1 do
// filter out properties that are events ( method pointer properties)
if not (PropList[i]^.PropType^.Kind = tkMethod) then
AStrings.Add
(Format('%s: %s', [PropList[i]^.Name, PropList[i]^.PropType^.Name])); // Now get properties that are events (method pointer properties)
NumProps := GetPropList(AClass.ClassInfo, [tkMethod], PropList);
if NumProps <> 0 then
begin
AStrings.Add('');
AStrings.Add(' EVENTS ================ ');
AStrings.Add('');
end;
// Fill the AStrings with the events.
for i := 0 to NumProps - 1 do
AStrings.Add(Format('%s: %s', [PropList[i]^.Name, PropList[i]^.PropType^.Name]));
finally
FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
end;
end;end;