请问一个GetTypeData函数的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 hxl126
  • 开始时间 开始时间
H

hxl126

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, typInfo, StdCtrls;

type
TClientField = Class(TObject)
private
FID: Integer;
FName: String;
FAddr: String;
FTel: String;
public
property ID: Integer read FID write FID;
property Name: String read FName write FName;
property Addr: String read FAddr write FAddr;
property Tel: String read FTel write FTel;
end;

TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

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^.PropType^.Kind of
tkClass : PropStr := '[Class] ';
tkMethod : PropStr := '[Method]';
tkSet : PropStr := '[Set] ';
tkEnumeration: PropStr := '[Enum] ';
else
PropStr := '[Field] ';
end;
PropStr := PropStr + PropList^.Name;
PropStr := PropStr + ': ' + PropList^.PropType^.Name;
AStrings.Add(PropStr);
end;
FreeMem(PropList);
end;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
GetClassProperties(TClientField, ListBox1.Items);
end;

end.

编译能通过,单击Button执行出错。如果GetClassProperties(TClientField, ListBox1.Items)中的TClientField改正TForm1, 或者TListBox都能执行正常,请问是什么原因?
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, typInfo, StdCtrls;

{$M+}

type
TClientField = Class(TObject)
private
FID: Integer;
FName: String;
FAddr: String;
FTel: String;
public
property ID: Integer read FID write FID;
property Name: String read FName write FName;
property Addr: String read FAddr write FAddr;
property Tel: String read FTel write FTel;
end;

TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

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^.PropType^.Kind of
tkClass : PropStr := '[Class] ';
tkMethod : PropStr := '[Method]';
tkSet : PropStr := '[Set] ';
tkEnumeration: PropStr := '[Enum] ';
else
PropStr := '[Field] ';
end;
PropStr := PropStr + PropList^.Name;
PropStr := PropStr + ': ' + PropList^.PropType^.Name;
AStrings.Add(PropStr);
end;
FreeMem(PropList);
end;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
GetClassProperties(TClientField, ListBox1.Items);
end;

end.

加上{$M+}后,执行不会出错,跟踪的时候,显示TClientField的属性为零,这是什么原因
 
后退
顶部