作者:狮子胯下 | 来源:互联网 | 2023-05-19 02:58
procedure GetBaseClassInfo(AClass: TObject; AStrings: TStrings);
{ This method obtains some basic RTTI data from the given
object
and adds that
information to the AStrings parameter. }
var
ClassTypeInfo: PTypeInfo;
ClassTypeData: PTypeData;
EnumName: String;
begin
ClassTypeInfo :
=
AClass.ClassInfo;
ClassTypeData :
=
GetTypeData(ClassTypeInfo);
with AStrings
do
begin
Add(Format(
'
Class Name: %s
'
, [ClassTypeInfo.Name]));
EnumName :
=
GetEnumName(TypeInfo(TTypeKind), Integer(ClassTypeInfo.Kind));
Add(Format(
'
Kind: %s
'
, [EnumName]));
Add(Format(
'
Size: %d
'
, [AClass.InstanceSize]));
Add(Format(
'
Defined in: %s.pas
'
, [ClassTypeData.UnitName]));
Add(Format(
'
Num Properties: %d
'
,[ClassTypeData.PropCount]));
end;
end;
procedure GetClassProperties(AClass: TObject; AStrings: TStrings);
{ This method retrieves the property names and types
for
the given
object
and adds that information to the AStrings parameter. }
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);
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;
运用Delphi 的 RTTI可以实现 Java 与 C# 下的反射机制,只是大家在Delphi 下做 RAD开发做多了,没有关注到这些关键的技术,接下来会写一个Delphi 下的OR Mapping框架,到时候会与大家分享。