Delphi 下的RTTI

来源:互联网 发布:java存储过程调用 编辑:程序博客网 时间:2024/03/29 16:23
目录
===============================================================================
⊙ RTTI 简介
⊙ 类(class) 和 VMT 的关系
⊙ 类(class)、类的类(class of class)、类变量(class variable) 的关系
⊙ TObject.ClassType 和 TObject.ClassInfo
⊙ is 和 as 运算符的原理
⊙ TTypeInfo – RTTI 信息的结构
⊙ 获取类(class)的属性(property)信息
⊙ 获取方法(method)的类型信息
⊙ 获取有序类型(ordinal)、集合(set)类型的 RTTI 信息
⊙ 获取其它数据类型的 RTTI 信息
===============================================================================
本文排版格式为:
    正文由窗口自动换行;所有代码以 80 字符为边界;中英文字符以空格符分隔。
(作者保留对本文的所有权利,未经作者同意请勿在在任何公共媒体转载。)

正文
===============================================================================
⊙ RTTI 简介
===============================================================================
RTTI(Run-Time Type Information) 翻译过来的名称是“运行期类型信息”,也就是说可以在运行期获得数据类型或类(class)的信息。这个 RTTI 到底有什么用处,我现在也说不清楚。我是在阅读 Delphi 持续机制的代码中发现了很多 RTTI 的运用,只好先把 RTTI 学习一遍。下面是我的学习笔记。如果你发现了错误请告诉我。谢谢!
Delphi 的 RTTI 主要分为类(class)的 RTTI 和一般数据类型的 RTTI,下面从类(class)开始。
===============================================================================
⊙ 类(class) 和 VMT 的关系
===============================================================================
一个类(class),从编译器的角度来看就是一个指向 VMT 的指针(在后文用 VMTptr 表示)。在类的 VMTptr 的负地址方向存储了一些类信息的指针,这些指针的值和指针所指的内容在编译后就确定了。比如 VMTptr - 44 的内容是指向类名称(ClassName)的指针。不过一般不使用数值来访问这些类信息,而是通过 System.pas 中定义的以 vmt 开头的常量,如 vtmClassName、vmtParent 等来访问。
类的方法有两种:对象级别的方法和类级别的方法。两者的 Self 指针意义是不同的。在对象级别的方法中 Self 指向对象地址空间,因此可以用它来访问对象的成员函数;在类级别的方法中 Self 指向类的 VMT,因此只能用它来访问 VMT 信息,而不能访问对象的成员字段。
===============================================================================
⊙ 类(class)、类的类(class of class)、类变量(class variable) 的关系
===============================================================================
上面说到类(class) 就是 VMTptr。在 Delphi 中还可以用 class of 关键字定义类的类,并且可以使用类的类定义类变量。从语法上理解这三者的关键并不难,把类当成普通的数据类型来考虑就可以了。在编译器级别上表现如何呢?
为了简化讨论,我们使用 TObject、TClass 和 TMyClass 来代表上面说的三种类型:
type
TClass = class of TObject;
var
TMyClass: TClass;
MyObject: TObject;
begin
TMyClass := TObject;
MyObject := TObject.Create;
MyObject := TClass.Create;
MyObject := TMyClass.Create;
end;

在上面的例子中,三个 TObject 对象都被成功地创建了。编译器的实现是:TObject 是一个 VMTPtr 常量。TClass 也是一个 VMTptr 常量,它的值就是 TObject。TMyClass 是一个 VMTptr 变量,它被赋值为 TObject。TObject.Create 与 TClass.Create 的汇编代码完全相同。但 TClass 不仅缺省代表一个类,而且还(主要)代表了类的类型,可以用它来定义类变量,实现一些类级别的操作。
===============================================================================
⊙ TObject.ClassType 和 TObject.ClassInfo
===============================================================================
function TObject.ClassType: TClass;
begin
Pointer(Result) := PPointer(Self)^;
end;
TObject.ClassType 是对象级别的方法,Self 的值是指向对象内存空间的指针,对象内存空间的前 4 个字节是类的 VMTptr。因此这个函数的返回值就是类的 VMTptr。
class function TObject.ClassInfo: Pointer;
begin
Result := PPointer(Integer(Self) + vmtTypeInfo)^;
end;
TObject.ClassInfo 使用 class 关键字定义,因此是一个类级别的方法。该方法中的 Self 指针就是 VMTptr。所以这个函数的返回值是 VMTptr 负方向的 vmtTypeInfo 的内容。
TObject.ClassInfo 返回的 Pointer 指针,实际上是指向类的 RTTI 结构的指针。但是不能访问 TObject.ClassInfo 指向的内容(TObject.ClassInfo 返回值是 0),因为 Delphi 只在 TPersistent 类及 TPersistent 的后继类中产生 RTTI 信息。(从编译器的角度来看,这是在 TPersistent 类的声明之前使用 {$M+} 指示字的结果。)
TObject 还定义了一些获取类 RTTI 信息的函数,列举在下,就不一一分析了:
TObject.ClassName: ShortString;   类的名称
TObject.ClassParent: TClass;      对象的父类
TObject.InheritsFrom: Boolean;    是否继承自某类
TObject.InstanceSize: Longint;    对象实例的大小
===============================================================================
⊙ is 和 as 运算符的原理
===============================================================================
我们知道可以在运行期使用 is 关键字判断一个对象是否属于某个类,可以使用 as 关键字把某个对象安全地转换为某个类。在编译器的层次上,is 和 as 的操作是由 System.pas 中两个函数完成的。
{ System.pas }
function _IsClass(Child: TObject; Parent: TClass): Boolean;
begin
Result := (Child <> nil) and Child.InheritsFrom(Parent);
end;
_IsClass 很简单,它使用 TObject 的 InheritsForm 函数判断该对象是否是从某个类或它的父类中继承下来的。每个类的 VMT 中都有一项 vmtParent 指针,指向该类的父类的 VMT。TObject.InheritsFrom 实际上是通过[递归]判断父类 VMT 指针是否等于自己的 VMT 指针来判断是否是从该类继承的。
{ System.pas }
class function TObject.InheritsFrom(AClass: TClass): Boolean;
var
ClassPtr: TClass;
begin
ClassPtr := Self;
while (ClassPtr <> nil) and (ClassPtr <> AClass) do
    ClassPtr := PPointer(Integer(ClassPtr) + vmtParent)^;
Result := ClassPtr = AClass;
end;
as 操作符实际上是由 System.pas 中的 _AsClass 函数完成的。它简单地调用 is 操作符判断对象是否属于某个类,如果不是就触发异常。虽然 _AsClass 返回值为 TObject 类型,但编译器会自动把返回的对象改变为 Parent 类,否则返回的对象没有办法使用 TObject 之外的方法和数据。
{ System.pas }
function _AsClass(Child: TObject; Parent: TClass): TObject;
begin
Result := Child;
if not (Child is Parent) then
    Error(reInvalidCast);   // loses return address
end;
===============================================================================
⊙ TTypeInfo – RTTI 信息的结构
===============================================================================
RTTI 信息的结构定义在 TypInfo.pas 中:
TTypeInfo = record        // TTypeInfo 是 RTTI 信息的结构
    Kind: TTypeKind;        // RTTI 信息的数据类型
    Name: ShortString;      // 数据类型的名称
   {TypeData: TTypeData}    // RTTI 的内容
end;
TTypeInfo 就是 RTTI 信息的结构。TObject.ClassInfo 返回指向存放 class TTypeInfo 信息的指针。Kind 是枚举类型,它表示 RTTI 结构中所包含数据类型。Name 是数据类型的名称。注意,最后一个字段 TypeData 被注释掉了,这说明该处的结构内容根据不同的数据类型有所不同。
TTypeKind 枚举定义了可以使用 RTTI 信息的数据类型,它几乎包含了所有的 Delphi 数据类型,其中包括 tkClass。
TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
    tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,
    tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray);
TTypeData 是个巨大的记录类型,在此不再列出,后文会根据需要列出该记录的内容。
===============================================================================
⊙ 获取类(class)的属性(property)信息
===============================================================================
这一段是 RTTI 中最复杂的部分,努力把本段吃透,后面的内容都是非常简单的。
下面是一个获取类的属性的例子:
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。
===============================================================================
⊙ 获取方法(method)的类型信息
===============================================================================
所谓方法就是以 of object 关键字声明的函数指针,下面的函数可以显示一个方法的类型信息:
procedure GetMethodTypeInfo(ATypeInfo: PTypeInfo; AStrings: TStrings);
type
PParamData = ^TParamData;
TParamData = record       // 函数参数的数据结构
    Flags: TParamFlags;     // 参数传递规则
    ParamName: ShortString; // 参数的名称
    TypeName: ShortString; // 参数的类型名称
end;
function GetParamFlagsName(AParamFlags: TParamFlags): string;
var
    I: Integer;
begin
    Result := '';
    for I := Integer(pfVar) to Integer(pfOut) do begin
      if I = Integer(pfAddress) then Continue;
      if TParamFlag(I) in AParamFlags then
        Result := Result + ' ' + GetEnumName(TypeInfo(TParamFlag), I);
    end;
end;
var
MethodTypeData: PTypeData;
ParamData: PParamData;
TypeStr: PShortString;
I: Integer;
begin
MethodTypeData := GetTypeData(ATypeInfo);
AStrings.Add('---------------------------------');
AStrings.Add('Method Name: ' + ATypeInfo^.Name);
AStrings.Add('Method Kind: ' + GetEnumName(TypeInfo(TMethodKind),
    Integer(MethodTypeData^.MethodKind)));
AStrings.Add('Params Count: '+ IntToStr(MethodTypeData^.ParamCount));
AStrings.Add('Params List:');
ParamData := PParamData(@MethodTypeData^.ParamList);
for I := 1 to MethodTypeData^.ParamCount do
begin
    TypeStr := Pointer(Integer(@ParamData^.ParamName) +
      Length(ParamData^.ParamName) + 1);
    AStrings.Add(Format(' [%s] %s: %s',[GetParamFlagsName(ParamData^.Flags),
      ParamData^.ParamName, TypeStr^]));
    ParamData := PParamData(Integer(ParamData) + SizeOf(TParamFlags) +
      Length(ParamData^.ParamName) + Length(TypeStr^) + 2);
end;
if MethodTypeData^.MethodKind = mkFunction then
    AStrings.Add('Result Value: ' + PShortString(ParamData)^);
end;
作为实验,在表单上放置一个 TListBox,然后执行以下代码,观察执行结果:
type
TMyMethod = function(A: array of Char; var B: TObject): Integer of object;
procedure TForm1.FormCreate(Sender: TObject);
begin
GetMethodTypeInfo(TypeInfo(TMyMethod), ListBox1.Items);
GetMethodTypeInfo(TypeInfo(TMouseEvent), ListBox1.Items);
GetMethodTypeInfo(TypeInfo(TKeyPressEvent), ListBox1.Items);
GetMethodTypeInfo(TypeInfo(TMouseWheelEvent), ListBox1.Items);
end;
由于获取方法的类型信息比较复杂,我尽量压缩代码也还是有这么长,让我们看看它的实现原理。GetMethodTypeInfo 的第一个参数是 PTypeInfo 类型,表示方法的类型信息地址。第二个参数是一个字符串列表,可以使用任何实现 TStrings 操作的对象。我们可以使用 System.pas 中的 TypeInfo 函数获得任何类型的 RTTI 信息指针。TypeInfo 函数像 SizeOf 一样,是内置于编译器中的。
GetMethodTypeInfo 还用到了 TypInfo.pas 中的 GetEnumName 函数。这个函数通过枚举类型的整数值得到枚举类型的名称。
function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
与获取类(class)的属性信息类似,方法的类型信息也在 TTypeData 结构中
TTypeData = packed record
    case TTypeKind of
      tkMethod: (
        MethodKind: TMethodKind;            // 方法指针的类型
        ParamCount: Byte;                   // 参数数量
        ParamList: array[0..1023] of Char   // 参数详细信息,见下行注释
       {ParamList: array[1..ParamCount] of
          record
            Flags: TParamFlags;             // 参数传递规则
            ParamName: ShortString;         // 参数的名称
            TypeName: ShortString;          // 参数的类型
          end;
        ResultType: ShortString});          // 返回值的名称
end;
TMethodKind 是方法的类型,定义如下:
TMethodKind = (mkProcedure, mkFunction, mkConstructor, mkDestructor,
    mkClassProcedure, mkClassFunction,
    { Obsolete }
    mkSafeProcedure, mkSafeFunction);
TParamsFlags 是参数传递的规则,定义如下:
TParamFlag = (pfVar, pfConst, pfArray, pfAddress, pfReference, pfOut);
TParamFlags = set of TParamFlag;
由于 ParamName 和 TypeName 是变长字符串,不能直接取用该字段的值,而应该使用指针步进的方法,取出参数信息,所以上面的代码显得比较长。
===============================================================================
⊙ 获取有序类型(ordinal)、集合(set)类型的 RTTI 信息
===============================================================================
讨论完了属性和方法的 RTTI 信息之后再来看其它数据类型的 RTTI 就简单多了。所有获取 RTTI 的原理都是通过 GetTypeData 函数得到 TTypeData 的指针,再通过 TTypeInfo.TypeKind 来解析 TTypeData。任何数据类型的 TTypeInfo 指针可以通过 TypeInfo 函数获得。
有序类型的 TTypeData 定义如下:
TTypeData = packed record
tkInteger, tkChar, tkEnumeration, tkSet, tkWChar: (
    OrdType: TOrdType;         // 有序数值类型
    case TTypeKind of
      case TTypeKind of
        tkInteger, tkChar, tkEnumeration, tkWChar: (
          MinValue: Longint;   // 类型的最小值
          MaxValue: Longint;   // 类型的最大值
          case TTypeKind of
            tkInteger, tkChar, tkWChar: ();
            tkEnumeration: (
              BaseType: PPTypeInfo;      // 指针的指针,它指向枚举的 PTypeInfo
              NameList: ShortStringBase;     // 枚举的名称字符串(不能直接取用)
              EnumUnitName: ShortStringBase)); // 所在的单元名称(不能直接取用)
          tkSet: (
            CompType: PPTypeInfo));            // 指向集合基类 RTTI 指针的指针
end;
下面是一个获取有序类型和集合类型的 RTTI 信息的函数:
procedure GetOrdTypeInfo(ATypeInfo: PTypeInfo; AStrings: TStrings);
var
OrdTypeData: PTypeData;
I: Integer;
begin
OrdTypeData := GetTypeData(ATypeInfo);
AStrings.Add('------------------------------------');
AStrings.Add('Type Name: ' + ATypeInfo^.Name);
AStrings.Add('Type Kind: ' + GetEnumName(TypeInfo(TTypeKind),
    Integer(ATypeInfo^.Kind)));
AStrings.Add('Data Type: ' + GetEnumName(TypeInfo(TOrdType),
    Integer(OrdTypeData^.OrdType)));
if ATypeInfo^.Kind <> tkSet then begin
    AStrings.Add('Min Value: ' + IntToStr(OrdTypeData^.MinValue));
    AStrings.Add('Max Value: ' + IntToStr(OrdTypeData^.MaxValue));
end;
if ATypeInfo^.Kind = tkSet then
    GetOrdTypeInfo(OrdTypeData^.CompType^, AStrings);
if ATypeInfo^.Kind = tkEnumeration then
    for I := OrdTypeData^.MinValue to OrdTypeData^.MaxValue do
      AStrings.Add(Format(' Value %d: %s', [I, GetEnumName(ATypeInfo, I)]));
end;
在表单上放置一个 TListBox,运行以下代码查看结果:
type TMyEnum = (EnumA, EnumB, EnumC);
procedure TForm1.FormCreate(Sender: TObject);
begin
GetOrdTypeInfo(TypeInfo(Char), ListBox1.Items);
GetOrdTypeInfo(TypeInfo(Integer), ListBox1.Items);
GetOrdTypeInfo(TypeInfo(TFormBorderStyle), ListBox1.Items);
GetOrdTypeInfo(TypeInfo(TBorderIcons), ListBox1.Items);
GetOrdTypeInfo(TypeInfo(TMyEnum), ListBox1.Items);
end;
(如果枚举元素没有按缺省的 0 基准定义,那么将不能产生 RTTI 信息,为什么?)
===============================================================================
⊙ 获取其它数据类型的 RTTI 信息
===============================================================================
上面讨论了几个典型的 RTTI 信息的运行,其它的数据类型的 RTTI 信息的获取方法与上面类似。由于这些操作更加简单,就不一一讨论。下面概述其它类型的 RTTI 信息的情况:
LongString、WideString 和 Variant 没有 RTTI 信息;
ShortString 只有 MaxLength 信息;
浮点数类型只有 FloatType: TFloatType 信息;
TFloatType = (ftSingle, ftDouble, ftExtended, ftComp, ftCurr);
Int64 只有最大值和最小值信息(也是 64 位整数表示);
Interface 和动态数组不太熟悉,就不作介绍了。
===============================================================================
⊙ 结束
===============================================================================

implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.btnShowClick(Sender: TObject);
begin
if FindClass('TForm2') <> nil then
   ShowMessage('有了 :)');
end;
procedure TForm1.btnRegisterClick(Sender: TObject);
begin
RegisterClass(TForm2);
end;
先不要按 btnRegister 这个按钮,直接按 btnShow 发现提示错误。

⊙ SetOrdProp 函数
===============================================================================
SetOrdProp 函数是 GetOrdProp 的逆过程,它调用 TPropInfo.SetProc 函数指针设置对象的属性值。SetProc 指针的第一个字节的意义同 GetProc 一样,也是表示该 SetProc 是字段偏移、虚方法偏移和静态方法。
procedure SetOrdProp(Instance: TObject; PropInfo: PPropInfo; Value: Longint);
SetOrdProc 也根据属性名称重载了:
procedure SetOrdProp(Instance: TObject; const PropName: string; Value: Longint);
由于 SetOrdProp 的汇编代码与 GetOrdProp 的几乎一样,在此就不再列出。作为练习,试用一下:
SetOrdProp(Self, 'Height', Self.Height + 10);
该语句的功能相当于:
Self.Height := Self.Height + 10;
* 后文介绍的 Set___Prop 系列函数或者调用本函数,或者它的实现方法与本函数类似。
===============================================================================
⊙ GetEnumProp / SetEnumProp 函数
===============================================================================
GetEnumProp 函数获取枚举类型属性的枚举字符串,它调用 GetEnumName 转换 GetOrdProp 的返回值。
function GetEnumProp(Instance: TObject; PropInfo: PPropInfo): string;
function GetEnumProp(Instance: TObject; const PropName: string): string;
SetEnumProp 函数使用枚举字符串设置枚举类型属性值,它调用 GetEnumValue 转换枚举字符串后再调用 SetOrdProp 设置属性值。
procedure SetEnumProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: string);
procedure SetEnumProp(Instance: TObject; const PropName: string;
    const Value: string);
===============================================================================
⊙ GetSetProp / SetSetProp 函数
===============================================================================
GetSetProp 函数用于获取集合类型属性的字符串值,它也是调用 GetOrdProp 获得属性值,然后调用 SetToString 函数把数值转换成字符串。
注意:GetOrdProp 函数返回值是 Integer,那么它是如何表示可以存储 256 个元素的集合类型呢?答案是:如果是 published 集合属性,那么该集合最大只能是 4 个字节,也就是最多只能存储 32 个元素。
function GetSetProp(Instance: TObject; PropInfo: PPropInfo;
    Brackets: Boolean): string;
function GetSetProp(Instance: TObject; const PropName: string;
    Brackets: Boolean = False): string;
SetSetProp 函数用于通过字符串设置集合类型属性的值。它先调用 StringToSet 函数把字符串转换为整数值,然后使用 SetOrdProp 函数设置属性值。
procedure SetSetProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: string);
procedure SetSetProp(Instance: TObject; const PropName: string;
    const Value: string);
试验: SetSetProp(Self, 'BorderIcons', '[biSystemMenu]');

===============================================================================
⊙ GetObjectProp / SetObjectProp 函数
===============================================================================
对象实际上是指针,也就是整数值,所以 GetObjectProp 直接调用 GetOrdProp 就可以了。
MinClass 参数指定得到的 Object 必须属于某个 class ,如果不是则返回 nil 。
function GetObjectProp(Instance: TObject; PropInfo: PPropInfo;
    MinClass: TClass = nil): TObject;
function GetObjectProp(Instance: TObject; const PropName: string;
    MinClass: TClass = nil): TObject;
SetObjectProp 用于设置属性的对象句柄。ValidateClass 参数表示是否需要检查传入的对象类型与属性信息的类信息是否兼容。
procedure SetObjectProp(Instance: TObject; PropInfo: PPropInfo;
    Value: TObject; ValidateClass: Boolean = True);
procedure SetObjectProp(Instance: TObject; const PropName: string;
    Value: TObject);
例子:
var
    MyFont: TFont;
begin
    MyFont := TFont.Create;
    MyFont.Height := 20;
    SetObjectProp(Self, 'Font', MyFont);
end;
===============================================================================
⊙ GetStrProp / SetStrProp 函数
===============================================================================
GetStrProp 函数用于获得字符串类型的属性值。
function GetStrProp(Instance: TObject; PropInfo: PPropInfo): string;
function GetStrProp(Instance: TObject; const PropName: string): string;
由于 Delphi 支持三种类型的字符串,GetStrProp 根据字符串的类型,分别调用三个获得字符串属性值的函数:
case PropInfo^.PropType^.Kind of
    tkString: GetShortStrPropAsLongStr(Instance, PropInfo, Result);
    tkLString: GetLongStrProp(Instance, PropInfo, Result);
    tkWString: GetWideStrPropAsLongStr(Instance, PropInfo, Result);
end;
其中 GetShortStrPropAsLongStr 又调用了 GetShortStrProp;GetWideStrPropAsLongStr 又调用了 GetWideStrProp,进行字符串间的类型转换。
SetStrProp 函数用于设置字符串类型的属性值。它的实现方法与 GetStrProp 类似。
procedure SetStrProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: string);
procedure SetStrProp(Instance: TObject; const PropName: string;
    const Value: string);
===============================================================================
⊙ GetFloatProp / SetFloatProp 函数
===============================================================================
GetFloatProp 用于获得浮点型属性值。它将 Single(4 bytes)、Double(8 bytes)、Comp(8 bytes)、Currency(8 bytes) 类型的浮点数属性转换为 Extented(10 bytes) 类型返回。
function GetFloatProp(Instance: TObject; PropInfo: PPropInfo): Extended;
function GetFloatProp(Instance: TObject; const PropName: string): Extended;
SetFloatProp 用于设置浮点型属性值。它的实现方法与 GetFloatProp 类似。
procedure SetFloatProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: Extended);
procedure SetFloatProp(Instance: TObject; const PropName: string;
    const Value: Extended);
===============================================================================
⊙ GetVariantProp / SetVariantProp
===============================================================================
GetVariantProp 函数用于获得 Variant 类型的属性值。
function GetVariantProp(Instance: TObject; PropInfo: PPropInfo): Variant;
function GetVariantProp(Instance: TObject; const PropName: string): Variant;
SetVariantProp 函数用于设置 Variant 类型的属性值。
procedure SetVariantProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: Variant);
procedure SetVariantProp(Instance: TObject; const PropName: string;
    const Value: Variant);
===============================================================================
⊙ GetMethodProp / SetMethodProp
===============================================================================
GetMethodProp 函数用于获得 Method 类型的属性值。
function GetMethodProp(Instance: TObject; PropInfo: PPropInfo): TMethod;
function GetMethodProp(Instance: TObject; const PropName: string): TMethod;
SetMethodProp 函数用于设置 Method 类型的属性值。
procedure SetMethodProp(Instance: TObject; const PropName: string;
    const Value: TMethod);
procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: TMethod);
===============================================================================
⊙ GetInt64Prop / SetInt64Prop
===============================================================================
SetInt64Prop 函数用于设置 Int64 类型的属性值。不同于一般整数用 EAX 返回,Int64 类型的返回值由 EDX:EAX 返回,所以有必要单独定义 Int64 的获取和设置方法。
function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;
function GetInt64Prop(Instance: TObject; const PropName: string): Int64;
SetInt64Prop 函数用于设置 Int64 类型的属性值。
procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo;
    const Value: Int64);
procedure SetInt64Prop(Instance: TObject; const PropName: string;
    const Value: Int64);
===============================================================================
⊙ GetInterfaceProp / SetInterfaceProp 函数
===============================================================================
GetInterfaceProp 函数用于获得 Interface 类型的属性值。
function GetInterfaceProp(Instance: TObject; PropInfo: PPropInfo): IInterface;
function GetInterfaceProp(Instance: TObject; const PropName: string): IInterface;
SetInterfaceProp 函数用于设置 Interface 类型的属性值。
procedure SetInterfaceProp(Instance: TObject; PropInfo: PPropInfo;
    const Value: IInterface);
procedure SetInterfaceProp(Instance: TObject; const PropName: string;
    const Value: IInterface);
* 不太熟悉 Interface,以后再看实现过程。
===============================================================================
⊙ GetPropValue / SetPropValue 函数
===============================================================================
GetPropValue 函数用于获得任何类型的属性值,它返回 Variant 类型。
注意,这个函数没有重载函数,只能使用属性名称字符串为参数。
GetPropValue 先调用 GetPropInfo 函数获得属性的类型,然后根据属性的数据类型选择调用以上介绍的 GetOrdProp、GetEnumProp、GetSetProp、GetStrProp 等函数实现具体的功能。
GetPropValue 的参数 PreferStrings 如果设置为 True,那么对于枚举、集合类型,将返回字符串值,否则返回整数值。GetPropValue 还可以返回动态数组类型的属性值。(目前对动态数组不太熟悉,先记下来。)
function GetPropValue(Instance: TObject; const PropName: string;
    PreferStrings: Boolean): Variant;
SetPropValue 函数用于设置任何类型的属性值。SetPropValue 的实现与 GetPropValue 类似。并且 SetPropValue 内部分析 Value 参数是否是字符串来设置枚举和集合类型的属性,所以不需要 PreferStrings 参数。SetPropValue 也可以设置动态数组属性,它使用了 SetOrdProp 函数实现这一功能,看来动态数组在内存中的表现是一个指针。
procedure SetPropValue(Instance: TObject; const PropName: string;
    const Value: Variant);
===============================================================================
⊙ TPublishableVariantType class
===============================================================================
在 TypInfo.pas 的代码注释中说 TPublishableVariantType 是用来代替 TCustomVariantType 以便更容易在 RTTI 中使用自定义的 Variant 类型。
* 现在对这两个类型都不太了解,先记在这里以后再学。
===============================================================================
⊙ RegisterClass / FindClass 系列函数 (Classes.pas)
===============================================================================
Delphi 提供了一种机制,可以使用类(class)的名称获得类(class VMTptr)。缺省情况下这些类必须是从 TPersistent 类继承下来的。使用这项功能之前必须在先把类信息注册到全局对象 RegGroup 中。
RegisterClass 函数用于注册类信息至 RegGroup 中,注意该函数名称和 Win32 API 中注册窗口类的函数同名。如果类已经被注册过了,RegisterClass 将直接返回。如果有一个不同的类以相同的名称注册了,RegisterClass 将触发异常(EFilerError)。
procedure RegisterClass(AClass: TPersistentClass);
RegisterClasses 函数可以方便地注册一批类:
procedure RegisterClasses(AClasses: array of TPersistentClass);
RegisterClassAlias 函数可以为类以其它的名称注册,以避免名称冲突。
procedure RegisterClassAlias(AClass: TPersistentClass; const Alias: string);
GetClass 函数根据类名称字符串获得类(class),如果没找到,将返回 nil:
function GetClass(const AClassName: string): TPersistentClass;
FindClass 函数包装了 GetClass,不同的是如果没找到该类,则触发异常(EClassNotFound):
function FindClass(const ClassName: string): TPersistentClass;
UnRegisterClass 系列函数执行 RegisterClass 相反的工作:
procedure UnRegisterClass(AClass: TPersistentClass);
procedure UnRegisterClasses(AClasses: array of TPersistentClass);
procedure UnRegisterModuleClasses(Module: HMODULE);
缺省的 RegGroup 用于组织从 TPersistent 继承下来的类,下面五个函数可以设置自己的 RegGroup:
procedure StartClassGroup(AClass: TPersistentClass);
procedure GroupDescendentsWith(AClass, AClassGroup: TPersistentClass);
function ActivateClassGroup(AClass: TPersistentClass): TPersistentClass;
function ClassGroupOf(AClass: TPersistentClass): TPersistentClass; overload;
function ClassGroupOf(Instance: TPersistent): TPersistentClass; overload;
===============================================================================
⊙ IdentToInt / IntToIdent 系列函数 (Classes.pas)
===============================================================================
IdentToInt 和 IntToIdent 函数用于实现字符串值和数值之间的转换。它的原理很简单,就是通过数组一一映射查找。不过一般不用直接使用这两个函数,而是使用 Delphi 中已经包装好的函数。这些函数的返回值都是 Boolean,表示转换是否成功。
function IdentToInt(const Ident: string; var Int: Longint;
    const Map: array of TIdentMapEntry): Boolean;
function IntToIdent(Int: Longint; var Ident: string;
    const Map: array of TIdentMapEntry): Boolean;
{ Graphics.pas }
function CharsetToIdent(Charset: Longint; var Ident: string): Boolean;
function IdentToCharset(const Ident: string; var Charset: Longint): Boolean;
function ColorToIdent(Color: Longint; var Ident: string): Boolean;
function IdentToColor(const Ident: string; var Color: Longint): Boolean;
{ Controls.pas }
function CursorToIdent(Cursor: Longint; var Ident: string): Boolean;
function IdentToCursor(const Ident: string; var Cursor: Longint): Boolean;
例子:
var
    NewColor: Integer;
begin
    if IdentToColor('clWindow', NewColor) then
      Self.Color := NewColor;
end;

原创粉丝点击