Forum > General
How to invoke an arbitrary method using RTTI?
(1/1)
cpicanco:
Does Free Pascal ({$mode objfpc}) supports invoking arbitrary methods with RTTI?
Did Michael wrote a continuation of this documentation: https://www.freepascal.org/~michael/articles/rtti1/rtti1.pdf? He mentioned that invoking arbritrary methods would be explored in next chapters.
This Chat GPT answer did not worked:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses sysutils, rtti; type TMyClass = class procedure MyMethod; end; procedure TMyClass.MyMethod;begin writeln('MyMethod has been called');end; var ctx: TRttiContext; typ: TRttiType; method: TRttiMethod; instance: TObject;begin instance := TMyClass.Create; try ctx := TRttiContext.Create; try typ := ctx.GetType(TMyClass); method := typ.GetMethod('MyMethod'); if Assigned(method) then method.Invoke(instance, []); finally ctx.Free; end; finally instance.Free; end;end.
PS.: There is a recent question in StackOverflow on how to use the invoke method: https://stackoverflow.com/questions/78922855/in-lazarus-fpc-how-do-i-get-a-pointer-to-a-class-or-record-method/78927526?noredirect=1#comment139161075_78927526)
Navigation
[0] Message Index