How to get/create anonymous method from TRttiMethod?

Posted by Heinrich Ulbricht on Stack Overflow See other posts from Stack Overflow or by Heinrich Ulbricht
Published on 2012-06-04T10:36:54Z Indexed on 2012/06/04 10:40 UTC
Read the original article Hit count: 337

Filed under:
|
|

I want to handle a TRttiMethod as anonymous method. How could I do this?

Here is a simplified example of how I wish things to work:

Interface:

TMyClass = class
public
  // this method will be acquired via Rtti
  procedure Foo;

  // this method shall return above Foo as anonymous method
  function GetMethodAsAnonymous: TProc;
end;

Implementation:

function TMyClass.GetMethodAsAnonymous: TProc;
var
  Ctx: TRttiContext;
  RttiType: TRttiType;
  RttiMethod: TRttiMethod;
begin
  Ctx := TRttiContext.Create;
  try
    RttiType := Ctx.GetType(Self.ClassType);
    RttiMethod := RttiType.GetMethod('Foo');

    Result := ??????; // <-- I want to put RttiMethod here - but how?
  finally
    Ctx.Free;
  end;
end;

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-xe