能获取对象的信息,还有能修改对象的信息么?

解决方案 »

  1.   


    procedure TForm2.Button1Click(Sender: TObject);
    var
      Count: integer;
      i: integer;
      PropList: PPropList;
      str: string;
      VariantValue: Variant;
    begin
      listbox1.Items.Clear;
      count := GetPropList(Listbox1, PropList);
      for i:= 0 to count - 1 do
      begin
        str := PropList^[i]^.Name;
        if copy(str, 1, 2) <> 'On' then
        begin
          ListBox1.Items.Add(str);
        end;
      end;
    end;
      

  2.   

    procedure GetClassProperties(AClass: TClass; AStrings: TStrings);
    var
      PropCount, I: SmallInt;
      PropList: PPropList;
      PropStr: string;
    begin
      PropCount := GetTypeData(AClass.ClassInfo).PropCount;
      GetPropList(AClass.ClassInfo, PropList);
      for I := 0 to PropCount - 1 do
      begin
        case PropList[I]^.PropType^.Kind of
          tkClass      : PropStr := '[Class] ';
          tkMethod     : PropStr := '[Method]';
          tkSet        : PropStr := '[Set]   ';
          tkEnumeration: PropStr := '[Enum]  ';
        else
          PropStr := '[Field] ';
        end;
        PropStr := PropStr + PropList[I]^.Name;
        PropStr := PropStr + ': ' + PropList[I]^.PropType^.Name;
        AStrings.Add(PropStr);
      end;
      FreeMem(PropList);
    end;你可以在表单上放置一个 TListBox ,然后执行以下语句观察执行结果:  GetClassProperties(TForm1, ListBox1.Items);该函数先使用 GetTypeData 函数获得类的属性数量。GetTypeData 是 TypInfo.pas 中的一个函数,它的功能是返回 TTypeInfo 的 TypeData 数据的指针:{ TypInfo.pas }
    function GetTypeData(TypeInfo: PTypeInfo): PTypeData; assembler;class 的 TTypeData 结构如下:  TTypeData = packed record
        case TTypeKind of
          tkClass: (
            ClassType: TClass;         // 类 (VMTptr)
            ParentInfo: PPTypeInfo;    // 父类的 RTTI 指针
            PropCount: SmallInt;       // 属性数量
            UnitName: ShortStringBase; // 单元的名称
           {PropData: TPropData});     // 属性的详细信息
      end;其中的 PropData 又是一个大小可变的字段。TPropData 的定义如下:  TPropData = packed record
        PropCount: Word;       // 属性数量
        PropList: record end;  // 占位符,真正的意义在下一行
        {PropList: array[1..PropCount] of TPropInfo}
      end;每个属性信息在内存中的结构就是 TPropInfo,它的定义如下:  PPropInfo = ^TPropInfo;
      TPropInfo = packed record
        PropType: PPTypeInfo;    // 属性类型信息指针的指针
        GetProc: Pointer;        // 属性的 Get 方法指针
        SetProc: Pointer;        // 属性的 Set 方法指针
        StoredProc: Pointer;     // 属性的 StoredProc 指针
        Index: Integer;          // 属性的 Index 值
        Default: Longint;        // 属性的 Default 值
        NameIndex: SmallInt;     // 属性的名称索引(以 0 开始计数)
        Name: ShortString;       // 属性的名称
      end;为了方便访问属性信息,TypInfo.pas 中还定义了指向 TPropInfo 数组的指针:  PPropList = ^TPropList;
      TPropList = array[0..16379] of PPropInfo;我们可以使用 GetPropList 获得所有属性信息的指针数组,数组用完以后要记得用 FreeMem 把数组的内存清除。{ TypInfo.pas }
    function GetPropList(TypeInfo: PTypeInfo; out PropList: PPropList): Integer;GetPropList 传入类的 TTypeInfo 指针和 TPropList 的指针,它为 PropList 分配一块内存后把该内存填充为指向 TPropInfo 的指针数组,最后返回属性的数量。上面的例子演示了如何获得类的所有属性信息,也可以根据属性的名称单独获得属性信息:{ TypInfo.pas }
    function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string): PPropInfo;GetPropInfo 根据类的 RTTI 指针和属性的名称字符串,返回属性的信息 TPropInfo 的指针。如果没有找到该属性,则返回 nil。GetPropInfo 很容易使用,举个例子:  ShowMessage(GetPropInfo(TForm, 'Name')^.PropType^.Name);这句调用显示了 TForm 类的 Name 属性的类型名称:TComponentName
      

  3.   

    procedure GetClassProperties(AClass: TClass; AStrings: TStrings);
    var
      PropCount, I: SmallInt;
      PropList: PPropList;
      PropStr: string;
    begin
      PropCount := GetTypeData(AClass.ClassInfo).PropCount;
      GetPropList(AClass.ClassInfo, PropList);
      for I := 0 to PropCount - 1 do
      begin
        case PropList[I]^.PropType^.Kind of
          tkClass      : PropStr := '[Class] ';
          tkMethod     : PropStr := '[Method]';
          tkSet        : PropStr := '[Set]   ';
          tkEnumeration: PropStr := '[Enum]  ';
        else
          PropStr := '[Field] ';
        end;
        PropStr := PropStr + PropList[I]^.Name;
        PropStr := PropStr + ': ' + PropList[I]^.PropType^.Name;
        AStrings.Add(PropStr);
      end;
      FreeMem(PropList);
    end;你可以在表单上放置一个 TListBox ,然后执行以下语句观察执行结果:  GetClassProperties(TForm1, ListBox1.Items);该函数先使用 GetTypeData 函数获得类的属性数量。GetTypeData 是 TypInfo.pas 中的一个函数,它的功能是返回 TTypeInfo 的 TypeData 数据的指针:{ TypInfo.pas }
    function GetTypeData(TypeInfo: PTypeInfo): PTypeData; assembler;class 的 TTypeData 结构如下:  TTypeData = packed record
        case TTypeKind of
          tkClass: (
            ClassType: TClass;         // 类 (VMTptr)
            ParentInfo: PPTypeInfo;    // 父类的 RTTI 指针
            PropCount: SmallInt;       // 属性数量
            UnitName: ShortStringBase; // 单元的名称
           {PropData: TPropData});     // 属性的详细信息
      end;其中的 PropData 又是一个大小可变的字段。TPropData 的定义如下:  TPropData = packed record
        PropCount: Word;       // 属性数量
        PropList: record end;  // 占位符,真正的意义在下一行
        {PropList: array[1..PropCount] of TPropInfo}
      end;每个属性信息在内存中的结构就是 TPropInfo,它的定义如下:  PPropInfo = ^TPropInfo;
      TPropInfo = packed record
        PropType: PPTypeInfo;    // 属性类型信息指针的指针
        GetProc: Pointer;        // 属性的 Get 方法指针
        SetProc: Pointer;        // 属性的 Set 方法指针
        StoredProc: Pointer;     // 属性的 StoredProc 指针
        Index: Integer;          // 属性的 Index 值
        Default: Longint;        // 属性的 Default 值
        NameIndex: SmallInt;     // 属性的名称索引(以 0 开始计数)
        Name: ShortString;       // 属性的名称
      end;为了方便访问属性信息,TypInfo.pas 中还定义了指向 TPropInfo 数组的指针:  PPropList = ^TPropList;
      TPropList = array[0..16379] of PPropInfo;我们可以使用 GetPropList 获得所有属性信息的指针数组,数组用完以后要记得用 FreeMem 把数组的内存清除。{ TypInfo.pas }
    function GetPropList(TypeInfo: PTypeInfo; out PropList: PPropList): Integer;GetPropList 传入类的 TTypeInfo 指针和 TPropList 的指针,它为 PropList 分配一块内存后把该内存填充为指向 TPropInfo 的指针数组,最后返回属性的数量。上面的例子演示了如何获得类的所有属性信息,也可以根据属性的名称单独获得属性信息:{ TypInfo.pas }
    function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string): PPropInfo;GetPropInfo 根据类的 RTTI 指针和属性的名称字符串,返回属性的信息 TPropInfo 的指针。如果没有找到该属性,则返回 nil。GetPropInfo 很容易使用,举个例子:  ShowMessage(GetPropInfo(TForm, 'Name')^.PropType^.Name);这句调用显示了 TForm 类的 Name 属性的类型名称:TComponentName
      

  4.   

    加上use typinfo,我去学习一下了,谢谢啦,学会了来结贴