// Rtti调用类的方法
procedure TForm1.Button1Click(Sender: TObject);var obj: TMyClass; t: TRttiType; m1,m2: TRttiMethod; r: TValue; begin t := TRttiContext.Create.GetType(TMyClass); m1 := t.GetMethod('msg'); m2 := t.GetMethod('Add'); obj := TMyClass.Create(Self); m1.Invoke(obj, ['Delphi 2010']); r := m2.Invoke(obj, [1, 2]); ShowMessage(IntToStr(r.AsInteger)); obj.Free;end;// Rtti获取方法类型、返回值、参数uses Rtti,TypInfo;procedure TForm1.Button1Click(Sender: TObject);var ms: TArray<TRttiMethod>; m: TRttiMethod; mps: TArray<TRttiParameter>; mp: TRttiParameter;begin Memo1.Clear; ms := TRttiContext.Create.GetType(TButton).GetMethods; for m in ms do begin {方法名称} Memo1.Lines.Add('方法名称: ' + m.Name); {方法类型: proceedure、function 等} Memo1.Lines.Add('方法类型: ' + GetEnumName(TypeInfo(TMethodKind), Ord(m.MethodKind))); {方法的返回值类型} if Assigned(m.ReturnType) then Memo1.Lines.Add('返回值: ' + GetEnumName(TypeInfo(TTypeKind), Ord(m.ReturnType.TypeKind))); {方法的参数列表} mps := m.GetParameters; if Length(mps) > 0 then begin Memo1.Lines.Add('参数:'); for mp in mps do Memo1.Lines.Add(mp.ToString); //还可以通过 mp.ParamType 获取参数的数据类型 //还可以通过 mp.Flags 获取参数的修饰符(譬如 var、const 等) end; Memo1.Lines.Add(EmptyStr); end;end;// Rtti修改和获取类的属性procedure TForm1.Button2Click(Sender: TObject);var obj: TMyClass; t: TRttiType; p: TRttiProperty; r: TValue;begin obj := TMyClass.Create(Self); t := TRttiContext.Create.GetType(TMyClass); p := t.GetProperty('Name'); p.SetValue(obj, 'NewName'); r := p.GetValue(obj); ShowMessage(r.AsString); obj.Free;end;// Rtti设置枚举值procedure TForm1.Button3Click(Sender: TObject);var t: TRttiType; p: TRttiProperty; r: TValue;begin t := TRttiContext.Create.GetType(TButton); p := t.GetProperty('Align'); p.SetValue(Button1, TValue.FromOrdinal(TypeInfo(TAlign), Ord(alLeft))); r := p.GetValue(Button1); ShowMessage(IntToStr(r.AsOrdinal)); end;// Rtti获取类的字段procedure TForm1.Button4Click(Sender: TObject);var ctx: TRttiContext; t: TRttiType; f: TRttiField;begin Memo1.Clear; t := ctx.GetType(TButton); for f in t.GetFields do Memo1.Lines.Add(f.ToString);end;// Rtti获取结构体的信息procedure TForm1.Button1Click(Sender: TObject);var t: TRttiRecordType; f: TRttiField;begin Memo1.Clear; t := TRttiContext.Create.GetType(TypeInfo(TPoint)).AsRecord; Memo1.Lines.Add(t.QualifiedName); Memo1.Lines.Add(Format('Size: %d', [t.TypeSize])); Memo1.Lines.Add(EmptyStr); Memo1.Lines.Add(Format('字段数: %d', [Length(t.GetFields)])); Memo1.Lines.Add(Format('方法数: %d', [Length(t.GetMethods)])); Memo1.Lines.Add(Format('属性数: %d', [Length(t.GetProperties)])); Memo1.Lines.Add(EmptyStr); Memo1.Lines.Add('全部字段:'); for f in t.GetFields do Memo1.Lines.Add(f.ToString);end;// 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;