Delphi 7中的ClassInfo函数

穆罕默德(Mohamad)

当我在Delphi 7中定义这样的类时

  TPerson = class(TObject)
  private
    FLName: string;
    FFName: string;
    FAge: integer;
    FBDate: TDate;
  public
  published
    property FName: string read FFName write FFName;
    property LName: string read FLName write FLName;
    property Age: integer read FAge write FAge;
    property BDate: TDate read FBDate write FBDate;
  end;

procedure ListComponentProperties(AObject: TObject; Strings: TStrings);
var
  Count, Size, I: Integer;
  List: PPropList;
  PropInfo: PPropInfo;
  PropValue: string;
begin
  Count := GetPropList(AObject.ClassInfo, tkAny, List);
  Size  := Count * SizeOf(Pointer);
  GetMem(List, Size);
  try
    Count := GetPropList(AObject.ClassInfo, tkAny, List);
    for I := 0 to Count - 1 do
    begin
      PropInfo := List^[I];
      PropValue := VarToStr(GetPropValue(AObject, PropInfo^.Name));
    end;
  finally
    FreeMem(List);
  end;
end;

并且我想使用ListComponentProperties获取其已发布属性的列表,将显示一条错误消息。该错误与以下命令和AObject.ClassInfo有关

Count := GetPropList(AObject.ClassInfo, tkAny, List);

任何帮助将不胜感激。

大卫·赫弗南(David Heffernan)

您必须为该类型启用RTTI。默认情况下未启用。声明这样的类型:

type
  {$M+}
  TPerson = class(TObject)
  ....
  end;
  {$M-}

您最初致电GetPropList也是错误的。它必须显示为:

Count := GetPropList(AObject.ClassInfo, tkAny, nil);

如果启用警告,则编译器会告诉您您正在传递未初始化的变量。

我没有再检查您的代码。可能会有更多错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章