zoukankan      html  css  js  c++  java
  • 了解运行时类型信息(RTTI)

    RTTI需要引用单元TypeInfo

    GetPropInfo 函数用于获得属性的 RTTI 指针 PPropInfo。它有四种重载形式,后面三种重载的实现都是调用第一种形式。AKinds 参数用于限制属性的类型,如果得到的 PPropInfo 不属于指定的类型,则返回 nil。

    function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string): PPropInfo;

    function GetPropInfo(Instance: TObject; const PropName: string;AKinds: TTypeKinds = []): PPropInfo;

    function GetPropInfo(AClass: TClass; const PropName: string;AKinds: TTypeKinds = []): PPropInfo;

    function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string;AKinds: TTypeKinds): PPropInfo;

    //以下代码,循环修改窗体上的Button组件的Capiton
    //方法一:
    procedure TForm1.SetCaption;
    var
      pInfo : PPropInfo;
      i:integer;
    begin
      for i := 0 to Self.ControlCount - 1 do
      begin
        pInfo := GetPropInfo(Self.Controls[i],'Caption');   //GetPropInfo,根据'Caption'字符串,查找Caption属性
        if pInfo <> nil then                             //如果有
          TButton(Self.Controls[i]).Caption:= 'ABC';     //修改Capiton
      end;
    end;
    方法二:
    procedure TForm1.SetCaption;
    var
      pInfo : PPropInfo;
      i:integer;
    begin
      for i := 0 to Self.ControlCount - 1 do
      begin
        pInfo := GetPropInfo(Self.Controls[i],'Caption');
        if pInfo <> nil then
          SetPropValue(Self.Controls[i],'Caption','ABC');
      end;
    end;

    参考:http://www.cnblogs.com/key-ok/p/3358804.html

  • 相关阅读:
    大话设计模式C++ 适配器模式
    36. Valid Sudoku
    c++ 常用的数据结构
    《大话设计模式》c++实现 状态模式
    c++ 各种类型转换
    38. Count and Say
    《大话设计模式》c++实现 抽象工厂模式
    66. Plus One
    49. Group Anagrams
    《大话设计模式》c++实现 建造者模式
  • 原文地址:https://www.cnblogs.com/findumars/p/4748705.html
Copyright © 2011-2022 走看看