Disclaimer I personally will never use this code and never recommend using it.
Do it like this:
procedure Test2(Method: TProc); var Smth: TSomething; begin Smth:= TSomething.Create; Smth.Msg:= 'Hello Hack'; TMethod(Method).Data:= Smth; Method(); end;
Of course, this is still unsafe, because it will only work if what you put in Data is really compatible with this method.
Sergey asks:
What will you call your Test2 without creating a dummy instance of TSomething?
I suppose you can do it this way for static (i.e. non-virtual and non-dynamic) methods:
var Obj: TSomething; .... Test2(Obj.Show);
Of course, all this illustrates what a grotesque hack is. I think this is no better than the version in your question. There is no real clean way to do what you ask.
I suspect that the right way to solve your real problem would be to use RTTI to call the method.
source share