zoukankan      html  css  js  c++  java
  • Delphi 2010 新增功能之: Rtti 单元(3): TRttiOrdinalType


    任何数据类型中 Rtti 中都有对应的获取信息的类, 有序类型对应的是 TRttiOrdinalType.

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses Rtti;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      t: TRttiOrdinalType;
    begin
      Memo1.Clear;
    
      //先从类型名获取类型信息对象
      t := TRttiContext.Create.GetType(TypeInfo(Byte)) as TRttiOrdinalType;
      Memo1.Lines.Add(Format('%s - %s', [t.Name, t.QualifiedName]));
      Memo1.Lines.Add(Format('Size: %d', [t.TypeSize]));
      Memo1.Lines.Add('QualifiedName: ' + t.QualifiedName);
      Memo1.Lines.Add(Format('Min,Max: %d , %d', [t.MinValue, t.MaxValue]));
      Memo1.Lines.Add(EmptyStr); //空字串
    
      //可以用 AsOrdinal 方法代替前面的 as TRttiOrdinalType
      t := TRttiContext.Create.GetType(TypeInfo(Word)).AsOrdinal;
      Memo1.Lines.Add(Format('%s: %s', [t.Name, t.QualifiedName]));
      Memo1.Lines.Add(Format('Size: %d', [t.TypeSize]));
      Memo1.Lines.Add(Format('Min,Max: %d , %d', [t.MinValue, t.MaxValue]));
      Memo1.Lines.Add(EmptyStr);
    
      //也可以直接强制转换
      t := TRttiOrdinalType(TRttiContext.Create.GetType(TypeInfo(Integer)));
      Memo1.Lines.Add(Format('%s: %s', [t.Name, t.QualifiedName]));
      Memo1.Lines.Add(Format('Size: %d', [t.TypeSize]));
      Memo1.Lines.Add(Format('Min,Max: %d , %d', [t.MinValue, t.MaxValue]));
      Memo1.Lines.Add(EmptyStr);
    end;
    
    end.
    
  • 相关阅读:
    数组中找出最小的K个数
    温故而知新---Java(一)
    写一手简洁的代码
    同步IO与同步非阻塞IO的理解
    旋转打印矩阵
    求仅出现一次的最早字符
    spring 自定义schema 加载异常 White spaces are required between publicId and systemId.
    Java位运算知识点整理
    Idea 插件开发之DubboInvoke实践
    pinpoint插件开发实践
  • 原文地址:https://www.cnblogs.com/del/p/1584147.html
Copyright © 2011-2022 走看看