Recent

Author Topic: [SOLVED] Get name of assigned event handler thru rtti?  (Read 2529 times)

piola

  • Full Member
  • ***
  • Posts: 124
  • Lazarus 2.2, 64bit on Windows 8.1 x64
[SOLVED] Get name of assigned event handler thru rtti?
« on: January 05, 2022, 02:00:44 pm »
Hello,

I know how to use RTTI with enum types. This is very helpful. Now I'm facing the problem that I'd like to get the name of an assigned event handler for debugging purposes. Is this possible?

Example:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses Classes, SysUtils;
  4.  
  5. type TTest = class (TObject)
  6.    procedure MyTest (Sender: TObject); virtual; abstract;
  7. end;
  8.  
  9. var
  10.   p: TNotifyEvent;
  11.   t: TTest;
  12.  
  13. begin
  14.   t := TTest.Create;
  15.   p := @t.MyTest;
  16.   // wanted: pass p to an rtti call which returns the string "MyTest" or "TTest.MyTest"
  17.   FreeAndNil (t);
  18. end.
  19.  
« Last Edit: January 13, 2022, 11:05:56 pm by piola »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Get name of assigned event handler thru rtti?
« Reply #1 on: January 06, 2022, 01:43:05 pm »
Here you go, there are some requirements which I've mentioned in the code:

Code: Pascal  [Select][+][-]
  1. program trttifunc;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils;
  7.  
  8. type
  9.   { either enable type information or descend from a type that already has it
  10.     enabled (e.g. TPersistent, TComponent, TControl, etc.) }
  11.   {$M+}
  12.   TTest = class (TObject)
  13.   { only published methods can be handled this way as of now (this will change
  14.     in the future with Extended RTTI, but we're not quite there yet) }
  15.   published
  16.     procedure MyTest(Sender: TObject); virtual; {abstract;}
  17.     { note: don't use an abstract method for this as this might mess up things }
  18.   end;
  19.   {$M-}
  20.  
  21. procedure TTest.MyTest(Sender: TObject);
  22. begin
  23.  
  24. end;
  25.  
  26. { and here we go: }
  27. function GetMethodName(aMethod: TMethod): String;
  28. var
  29.   clsname, methodname: String;
  30. begin
  31.   clsname := TObject(aMethod.Data).ClassName;
  32.   methodname := TObject(aMethod.Data).MethodName(aMethod.Code);
  33.   if methodname = '' then
  34.     Result := ''
  35.   else
  36.     Result := clsname + '.' + methodname;
  37. end;
  38.  
  39. var
  40.   p: TNotifyEvent;
  41.   t: TTest;
  42.  
  43. begin
  44.   t := TTest.Create;
  45.   p := @t.MyTest;
  46.   // wanted: pass p to an rtti call which returns the string "MyTest" or "TTest.MyTest"
  47.   Writeln(GetMethodName(TMethod(p)));
  48.   FreeAndNil (t);
  49. end.
  50.  

piola

  • Full Member
  • ***
  • Posts: 124
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Get name of assigned event handler thru rtti?
« Reply #2 on: January 06, 2022, 09:07:12 pm »
Wow, great! Thank you very much!

 

TinyPortal © 2005-2018