RTTI(五)相关函数3【转自大富翁】

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;

==============================================================
⊙ 结束
==============================================================

posted on 2011-06-15 17:25  Tony Liu  阅读(540)  评论(0编辑  收藏  举报

导航