delphi - 使用RTTI时,我们如何获取和设置更深层次的子属性?

标签 delphi rtti delphi-xe7

概述

我很高兴之前已经有人问过几个类似的问题:

但是,我并没有进一步了解 RTTI 究竟如何用于满足我的需求。

我也花了很多时间和精力来写这个问题,所以我希望它不会被关闭:)

工作示例

下面有一些程序可以将组件的属性名称、值和类型输出到 TStrings 列表。原始来源不是我的,我只是对其做了一些小的更改,整理了代码并将它们放入一些整洁的可重用程序中:

<小时/>

下面将输出属性名称,例如:

  • Color
  • DoubleBuffered
  • Enabled
  • Height
  • Width
procedure GetComponentPropertyNames(Component: TComponent; OutList: TStrings);
var
  I: Integer;
  Count, Size: Integer;
  PropList: PPropList;
  PropInfo: PPropInfo;
begin
  OutList.BeginUpdate;
  try
    OutList.Clear;

    Count := GetPropList(Component.ClassInfo, tkAny, nil);
    Size  := Count * SizeOf(Pointer);
    GetMem(PropList, Size);
    try
      Count := GetPropList(Component.ClassInfo, tkAny, PropList);
      for I := 0 to Count -1 do
      begin
        PropInfo := PropList^[I];
        if not (PropInfo^.PropType^.Kind = tkMethod) then
        begin
          OutList.Add(PropInfo^.Name);
        end;
      end;
    finally
      FreeMem(PropList);
    end;
  finally
    OutList.EndUpdate;
  end;
end;
<小时/>

下面将输出属性值,例如:

  • clWindow
  • False
  • True
  • 25
  • 75
function GetComponentPropertyValue(Component: TComponent; APropName: string): string;
var
  I: Integer;
  Count, Size: Integer;
  PropList: PPropList;
  PropInfo: PPropInfo;
begin
  Count := GetPropList(Component.ClassInfo, tkAny, nil);
  Size  := Count * SizeOf(Pointer);
  GetMem(PropList, Size);
  try
    Count := GetPropList(Component.ClassInfo, tkAny, PropList);
    for I := 0 to Count -1 do
    begin
      PropInfo := PropList^[I];
      if not (PropInfo^.PropType^.Kind = tkMethod) then
      begin
        if SameText(PropInfo^.Name, APropName) then
        begin
          Result := System.Variants.VarToStr(GetPropValue(Component, PropInfo^.Name));
          Exit;
        end;
      end;
    end;
  finally
    FreeMem(PropList);
  end;
end;

procedure GetComponentPropertyValues(Component: TComponent; OutList: TStrings);
var
  SL: TStringList;
  I: Integer;
begin
  SL := TStringList.Create;
  try
    GetComponentPropertyNames(Component, SL);
    for I := 0 to SL.Count -1 do
    begin
      OutList.Add(GetComponentPropertyValue(Component, SL.Strings[I]));
    end;
  finally
    SL.Free;
  end;
end;
<小时/>

最后,下面将以字符串格式输出属性类型,例如:

  • TColor
  • Boolean
  • Boolean
  • Integer
  • Integer
function GetComponentPropertyType(Component: TComponent; APropName: string): string;
var
  SL: TStringList;
  I: Integer;
  PropInfo: TPropInfo;
  PropTypeName: string;
begin
  SL := TStringList.Create;
  try
    GetComponentPropertyNames(Component, SL);
    for I := 0 to SL.Count -1 do
    begin
      PropInfo := GetPropInfo(Component, SL.Strings[I])^;
      if SameText(PropInfo.Name, APropName) then
      begin
        PropTypeName := PropInfo.PropType^.Name;
        Result := PropTypeName;
        Exit;
      end;
    end;
  finally
    SL.Free;
  end;
end;

procedure GetComponentPropertyTypes(Component: TComponent; OutList: TStrings);
var
  SL: TStringList;
  I: Integer;
begin
  SL := TStringList.Create;
  try
    GetComponentPropertyNames(Component, SL);
    for I := 0 to SL.Count -1 do
    begin
      OutList.Add(GetComponentPropertyType(Component, SL.Strings[I]));
    end;
  finally
    SL.Free;
  end;
end;
<小时/>

并排显示上面调用的每个过程的输出将显示如下内容:

  • Color | clWindow | TColor
  • DoubleBuffered | False | Boolean
  • Enabled | True | Boolean
  • Height | 25 | Integer
  • Width | 75 | Integer
<小时/>

问题

此时以上所有内容都有效,除了我需要花一些时间多阅读文档以尝试更好地理解并能够消化所有内容之外,没有任何问题。

我的问题(这几天一直困扰着我)是如何正确获取和设置子属性。例如,看一下 Delphi 对象检查器的这个屏幕截图(根据目的进行了修改):

enter image description here

就像之前显示的过程一样,我需要对那些以蓝色突出显示的子属性执行相同的操作。

理想情况下,我想要的是一个可以传递组件和属性名称的函数,如果它有子属性,则返回 True,所以类似于:

function HasSubProperty(Component: TComponent; APropName: string): Boolean;
begin
  Result := ??
end;

我不确定这效果如何,但从屏幕截图中可以明显看出,某些子属性也有子属性(例如组件>字体>样式)。

最终我想要的是一种检索子属性的名称、值和类型的方法。所以类似:

procedure GetComponentSubPropertyNames(Component: TComponent; APropName: string;
  OutList: TStrings);
begin
  //
end;

调用时:

GetComponentSubPropertyNames(Label1, Anchors);

应该检索:

  • akLeft
  • akTop
  • akRight
  • akBottom

使用类似的过程来获取值和类型将如下所示:

  • akLeft | True | Boolean
  • akTop | True | Boolean
  • akRight | True | Boolean
  • akBottom | True | Boolean

对于字体子属性,例如:

  • Charset | DEFAULT_CHARSET | TFontCharset
  • Color | clWindowText | TColor
  • Height | -11 | Integer
  • Orientation | 0 | Integer
  • Pitch | fpDefault | TFontPitch
  • Quality | fqDefault | TFontQuality
  • Size | 8 | Integer

访问另一个子属性(Font.Style)会带来另一个问题,除非使用这样的过程:

procedure GetComponentSubPropertySubPropertyNames(Component: TComponent; APropName, ASubPropName: string; OutList: TStrings);
begin
  //
end;

这变得相当愚蠢。

<小时/>

摘要

基本上,我需要一种深入研究更深层次属性的方法,以获取它们的名称、值和类型,将它们放入列表中,并且还能够更改值。

如果有人可以编写一些代码示例来说明我如何实现这一目标(代码中带有一些注释的奖励),我将不胜感激。我确信对于某些人来说这将是一项相对容易的任务,但我发现它确实非常微不足道。

到目前为止,阅读了各种文档和示例,说实话,我仍然一无所知,主要关心的是不知道要使用什么类型或如何正确创建和管理它们。

最佳答案

诸如TFont、TAction、TPopupMenu之类的子属性已经是在诸如TButton之类的所有者组件中创建的组件(类)。

要了解属性类型,请使用 PropInfo.PropType^.Kind

查看Delphi帮助

TypInfo.PTypeInfo Type

TypInfo.TTypeKind

以下是您请求的代码示例:

function HasSubProperty(Component: TComponent; APropName: string): Boolean;
var PropInfo: TPropInfo;
begin
  PropInfo := GetPropInfo(Component, APropName)^;
  Result := PropInfo.PropType^.Kind in [tkClass,tkSet,tkRecord]
end;

获取子类的示例

function GetSubPropClass(Component: TComponent; APropName: string):TComponent;
    var PropInfo: PPropInfo;
        AObject : TObject;
    begin
      Result := nil;
      PropInfo := GetPropInfo(Component, APropName);
      if PropInfo.PropType^.Kind in [tkClass] then
      begin
        AObject := GetObjectProp(Component,PropInfo);
        if Assigned(AObject) then
          Result := TComponent(AObject);
      end;
    end;

使用示例

procedure TForm1.Button1Click(Sender: TObject);
var AComp : TComponent;
begin
  AComp := GetSubPropClass(Form1,'TFont',ListBox4.Items);
  if AComp <> nil then
    GetComponentPropertyNames(AComp);
end;

更新

这段代码将帮助您理解SET属性

function GetComponentPropertyValue(Component: TComponent; APropName: string): string;
var
  I,X: Integer;
  Count, Size: Integer;
  PropList: PPropList;
  PropInfo: PPropInfo;
  PropTypeInf : PTypeInfo;
  SetList : TStrings;
  SetName,SetVal : string;
begin
  Count := GetPropList(Component.ClassInfo, tkAny, nil);
  Size  := Count * SizeOf(Pointer);
  GetMem(PropList, Size);
  try
    Count := GetPropList(Component.ClassInfo, tkAny, PropList);
    for I := 0 to Count -1 do
    begin
     PropTypeInf := PropList^[I]^.PropType^;
     PropInfo := PropList^[I];
      if not (PropInfo^.PropType^.Kind = tkMethod) then
      begin
        if SameText(PropInfo^.Name, APropName) then
        begin

          if (PropInfo^.PropType^.Kind = tkSet) then
          begin
            try
              SetList := TStringList.Create;
              SetList.CommaText := System.Variants.VarToStr(GetPropValue(Component, PropInfo^.Name));
              for X := 0 to 255 do
              begin
                SetName := GetSetElementName(GetTypeData(PropTypeInf)^.CompType^,X);
                if ContainsStr(SetName,'UITypes') then break;
                SetVal := SetName + ' = ' + IfThen(SetList.IndexOf(SetName)<>-1,'True','False');
                if Result = '' then
                  Result := SetVal else
                  Result := Result + ', ' + SetVal;
              end;

            finally
              SetList.Free;
            end;
          end else
            Result := System.Variants.VarToStr(GetPropValue(Component, PropInfo^.Name));
          Exit;
        end;
      end;
    end;
  finally
    FreeMem(PropList);
  end;
end;

关于delphi - 使用RTTI时,我们如何获取和设置更深层次的子属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31297275/

相关文章:

delphi - Delphi 5调用C++ dll导致访问冲突

delphi - 在独立 EXE 和 Windows 服务之间切换

delphi - 处理大量行时,TMemo 速度慢得令人痛苦

使用 TValue 进行 Delphi 接口(interface)转换

delphi - 在柏林 10.1 开幕 XE7 项目 - 所有物体的比例和位置都不同

ios - Delphi XE6 在 iOS 中链接 C 代码

c++ - 我可以判断一个 std::type_info 对象是否等于另一个对象或从另一个对象派生的类吗?

c++ - 从函数返回后丢失 RTTI 信息

delphi - 帮助洞察中的新行

delphi - 为什么当 btn 为 NIL 时我可以访问 btn.Caption?