任何數據類型中 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.