Rtti 单元(3): 获取有序类型的信息

本文介绍如何使用RTTI(运行时类型信息)在Delphi中获取有序类型的信息,包括类型名称、大小以及取值范围等,并通过具体示例展示了不同类型的操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

任何数据类型中 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值