方法的更多信息是指: 方法类型、返回值、参数等.
--------------------------------------------------------------------------------
unit Unit1;interfaceuses
  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,TypInfo;procedure TForm1.Button1Click(Sender: TObject);
var
  ms: TArray<TRttiMethod>;
  m: TRttiMethod;
  mps: TArray<TRttiParameter>;
  mp: TRttiParameter;
begin
  Memo1.Clear;  {先获取方法集合, 这里随便使用了 TButton 类}
  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;end.http://www.cnblogs.com/del/archive/2009/10/16/1584401.html#1683401
===================================================================
我的问题是:
{先获取方法集合, 这里随便使用了 TButton 类}
ms := TRttiContext.Create.GetType(TButton).GetMethods;
这里的(TButton)能否通过变量赋值, 比如通过edit的text给它赋值,TRttiContext.Create.GetType(edit.text),就是这个意思,这样就通用了,请问行吗?
 

解决方案 »

  1.   

    当然可以,TRttiContext里面有这方面的方法:
        function GetType(ATypeInfo: Pointer): TRttiType; overload;
        function GetType(AClass: TClass): TRttiType; overload;
        function GetTypes: TArray<TRttiType>;
        function FindType(const AQualifiedName: string): TRttiType; <----这个
      

  2.   

    ZuoBaoquan,不会用,能否在指点一下?谢谢!需要引用什么东西吗?
      

  3.   

    //uses Rtti, StdCtrls;var
      context: TRttiContext;
      rttiType: TRttiType;
      method: TRttiMethod;
    begin
      with context.Create do
      begin
        rttiType := FindType('StdCtrls.TButton');
        if rttiType <> nil then
        for method in rttiType.GetMethods do
        begin
          Writeln(method.Name);
        end;
      end;
    end;
      

  4.   

    Writeln(method.Name); i/o error 105?delphi2010的错误提示
      

  5.   

    Writeln是控制台程序使用的输出函数,你如果用图形界面的话可以用ShowMessage等函数。