16,749
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TRec = record
name : string;
D: word;
end;
TRec2 = packed record
D: word;
end;
TMyTestClass = class(TPersistent)
private
FIID: TGUID;
FInt: Integer;
FMyRec: TRec;
FMyRec2: TRec2;
procedure SetMyRec(const Value: TRec);
procedure SetMyRec2(const Value: TRec2);
published // 公布了3个属性,其中一个 TGUID 类型
property IID: TGUID read FIID;
property IntVal: Integer read FInt;
property IntVal2: Integer read FInt;
property MyRec : TRec read FMyRec write SetMyRec;
property MyRec2 : TRec2 read FMyRec2 write SetMyRec2;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses TypInfo;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
tdata: PTypeData;
begin
tdata := GetTypeData(TMyTestClass.ClassInfo);
Showmessage(GUIDToString(tdata.Guid)); // 这里取得该类的 GUID
Showmessage(IntToStr(tdata.PropCount)); // 可以看到只有2个公布的属性
end;
{ TMyTestClass }
procedure TMyTestClass.SetMyRec(const Value: TRec);
begin
FMyRec := Value;
end;
procedure TMyTestClass.SetMyRec2(const Value: TRec2);
begin
FMyRec2 := Value;
end;
end.
unit Unit15;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyClass = class
private
FName: string;
FAge: integer;
FID: TGUID;
procedure SetAge(const Value: integer);
procedure SetID(const Value: TGUID);
procedure SetName(const Value: string);
published
property ID : TGUID read FID write SetID;
property Name : string read FName write SetName;
property Age : integer read FAge write SetAge;
end;
TMyClass2 = class
private
FName: string;
FAge: integer;
FID: TGUID;
procedure SetAge(const Value: integer);
procedure SetID(const Value: TGUID);
procedure SetName(const Value: string);
public
property ID : TGUID read FID write SetID;
property Name : string read FName write SetName;
property Age : integer read FAge write SetAge;
end;
TForm15 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form15: TForm15;
implementation
uses TypInfo, Rtti; //delphi 2010开始有Rtti, 能识别非published属性及过程,变量等
{$R *.dfm}
//2010前实现方法
function GetClzPropertys(AClz : TClass) : string;
var
vDataInfo : PTypedata;
vProps : PPropList;
i, vPropCount, vPropInfoSize : integer;
begin
result := '';
vDataInfo := GetTypeData(AClz.ClassInfo);
vPropCount := vDataInfo.PropCount;
vPropInfoSize := SizeOf(TpropInfo);
GetMem(vProps, vPropInfoSize * vPropCount);
try
GetPropInfos(AClz.ClassInfo, vProps);
for i := 0 to vPropCount - 1 do
begin
Result := result + vProps[i].Name + ',' + vProps[i].PropType^.Name + Chr(13);
end;
finally
FreeMem(vProps);
end;
end;
//2010及后实现方法
function GetClzPropertys2(AClz : TClass) : string;
var
ctx: TRttiContext;
vRttiType: TRttiType;
p: TRttiProperty;
begin
result := '';
vRttiType := ctx.GetType(AClz);
{
TMemberVisibility = (mvPrivate, mvProtected, mvPublic, mvPublished);
TRttiProperty.Visibility : TMemberVisibility
}
for p in vRttiType.GetProperties do
begin
Result := result + inttostr(Ord(p.Visibility)) + ' ' + p.ToString + Chr(13);
end;
end;
procedure TForm15.btn1Click(Sender: TObject);
begin
ShowMessage(GetClzPropertys(TMyClass));
ShowMessage(GetClzPropertys(TMyClass2));
ShowMessage(GetClzPropertys2(TMyClass));
ShowMessage(GetClzPropertys2(TMyClass2));
end;
{ TMyClass }
procedure TMyClass.SetAge(const Value: integer);
begin
FAge := Value;
end;
procedure TMyClass.SetID(const Value: TGUID);
begin
FID := Value;
end;
procedure TMyClass.SetName(const Value: string);
begin
FName := Value;
end;
{ TMyClass2 }
procedure TMyClass2.SetAge(const Value: integer);
begin
end;
procedure TMyClass2.SetID(const Value: TGUID);
begin
end;
procedure TMyClass2.SetName(const Value: string);
begin
end;
end.
TMyTestClass = class(TPersistent)
private
FIID: TGUID;
FInt: Integer;
published // 公布了3个属性,其中一个 TGUID 类型
property IID: TGUID read FIID;
property IntVal: Integer read FInt;
property IntVal2: Integer read FInt;
end;
......
var
tdata: PTypeData;
begin
tdata := GetTypeData(TMyTestClass.ClassInfo);
Showmessage(GUIDToString(tdata.Guid)); // 这里取得该类的 GUID
Showmessage(IntToStr(tdata.PropCount)); // 可以看到只有2个公布的属性
end;