我本来是在做com程序,但是需要引用的对象太多,而且重复代码量和大,所以我想获得一种方法能够得到对象的全部属性、方法、事件,及其动态调用时该属性的值(获得返回对象的名称)谢谢各位前辈!

解决方案 »

  1.   

    RTTI
    学习一下TypInfo单元里的内容。
      

  2.   

    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;
      

  3.   

    晕...
    var
      i,j :Integer;
      PropList :PPropList;
      //PropInfo :PPropInfo; 这个是没用的..忘删了..
    begin
      

  4.   

    我在Delphi7上怎么找不到这个函数呢?
    我找到的相关函数有以下这些(但是这些都没有例子)很可惜,希望各位大师指点!
    GetPropCount,GetPropDisplayString,GetProperties,GetProperty.....
    我其实只想获得引用的Com对象的属性、方法、事件的全部信息!谢谢!
      

  5.   

    为什么这里换了一个对象就不行了?
    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;
      

  6.   

    我找到了一段代码,可是这段代码不能枚举对象属性的值,请各位前辈帮我修改一样,谢谢!
    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;