Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Get name of assigned event handler thru rtti?
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
TStringList Question
by
mas steindorff
[
Today
at 07:56:39 pm]
Usage of IMMNotificationC...
by
Nevada Smith
[
Today
at 07:52:33 pm]
Removing duplicates from ...
by
BillMcEnaney
[
Today
at 07:07:24 pm]
[resolved] How to read ou...
by
Muso
[
Today
at 06:59:35 pm]
Optimizing the counter co...
by
BrunoK
[
Today
at 06:37:48 pm]
Lazarus Release 2.2.2
by
Dio Affriza
[
Today
at 06:23:11 pm]
2.2.2 Win x64: Listview i...
by
wp
[
Today
at 06:07:15 pm]
Another two bugs in TIpHt...
by
wp
[
Today
at 05:19:46 pm]
Cross Compiling
by
J-G
[
Today
at 04:51:50 pm]
gui application with long...
by
marcov
[
Today
at 04:48:51 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Get name of assigned event handler thru rtti? (Read 2234 times)
piola
Full Member
Posts: 104
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]
[+]
[-]
program
Project1
;
uses
Classes
,
SysUtils
;
type
TTest
=
class
(
TObject
)
procedure
MyTest
(
Sender
:
TObject
)
;
virtual
;
abstract
;
end
;
var
p
:
TNotifyEvent
;
t
:
TTest
;
begin
t
:
=
TTest
.
Create
;
p
:
=
@
t
.
MyTest
;
// wanted: pass p to an rtti call which returns the string "MyTest" or "TTest.MyTest"
FreeAndNil
(
t
)
;
end
.
«
Last Edit: January 13, 2022, 11:05:56 pm by piola
»
Logged
PascalDragon
Hero Member
Posts: 4008
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]
[+]
[-]
program
trttifunc
;
{$mode objfpc}{$H+}
uses
Classes
,
SysUtils
;
type
{ either enable type information or descend from a type that already has it
enabled (e.g. TPersistent, TComponent, TControl, etc.) }
{$M+}
TTest
=
class
(
TObject
)
{ only published methods can be handled this way as of now (this will change
in the future with Extended RTTI, but we're not quite there yet) }
published
procedure
MyTest
(
Sender
:
TObject
)
;
virtual
;
{abstract;}
{ note: don't use an abstract method for this as this might mess up things }
end
;
{$M-}
procedure
TTest
.
MyTest
(
Sender
:
TObject
)
;
begin
end
;
{ and here we go: }
function
GetMethodName
(
aMethod
:
TMethod
)
:
String
;
var
clsname
,
methodname
:
String
;
begin
clsname
:
=
TObject
(
aMethod
.
Data
)
.
ClassName
;
methodname
:
=
TObject
(
aMethod
.
Data
)
.
MethodName
(
aMethod
.
Code
)
;
if
methodname
=
''
then
Result
:
=
''
else
Result
:
=
clsname
+
'.'
+
methodname
;
end
;
var
p
:
TNotifyEvent
;
t
:
TTest
;
begin
t
:
=
TTest
.
Create
;
p
:
=
@
t
.
MyTest
;
// wanted: pass p to an rtti call which returns the string "MyTest" or "TTest.MyTest"
Writeln
(
GetMethodName
(
TMethod
(
p
)
)
)
;
FreeAndNil
(
t
)
;
end
.
Logged
piola
Full Member
Posts: 104
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!
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Get name of assigned event handler thru rtti?
TinyPortal
© 2005-2018