Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-07

How can I get a typeinfo by address?

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

I need to call a method dynamically by its name over string and pass some parameters to it. OK, I don't know if it is possibly in the current version of the compiler, but I'm trying to do something like this:

TMethodInvoker.Invoke(VMyObj, ['Param 1', 'Param 2', 1, True]);

I already get some paramerter informations using this code[1], that prints all parameters and its types using RTTI.

So I have a question: how can I get the method typedata looking for it in the VMT? Please see this:

  WriteLn(HexStr(@TMyObject.Test));
  WriteLn(HexStr(TMyObject.MethodAddress('Test')));

Prints the same address. But:

  TI := TypeInfo(@TMyObject.Test);
  WriteLn(HexStr(TI));
  TI := TypeInfo(TMyObject.MethodAddress('Test'));
  WriteLn(HexStr(TI));

Prints different results.

How System.TypeInfo retrieve the typeinfo from my @TMyObject.Test address? It is done by the method address so?

I took a look at the TRTTIWriter class and also in the VMT, but I don't know how the compiler handles this informations internally, so sorry it this question sounds nonsense. =P

[1] - http://pastebin.com/bPascrwe

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Graeme Geldenhuys
to silvioprog
silvioprog wrote
I need to call a method dynamically by its name over string and pass some parameters to it.

You mean like what FPCUnit, DUnit or FPTest unit testing frameworks do? If so, just take a look at any of them to see how they call the test methods.

Regards,
- Graeme -

Reply by email

Michael Van Canneyt
to silvioprog: I need to call a method dynamically by its name over string and pass some parameters to…
silvioprog wrote 21 lines
Hello, I need to call a method dynamically by its name over string and pass some parameters to it. OK, I don't know if it is possibly in the current version of the compiler, but I'm trying to do something like this: TMethodInvoker.Invoke(VMyObj, ['Param 1', 'Param 2', 1, True]); I already get some paramerter informations using this code[1], that prints all parameters and its types using RTTI. So I have a question: how can I get the method typedata looking for it in the VMT? Please see this:   WriteLn(HexStr(@TMyObject.Test));   WriteLn(HexStr(TMyObject.MethodAddress('Test'))); Prints the same address. But:   TI := TypeInfo(@TMyObject.Test);   WriteLn(HexStr(TI));   TI := TypeInfo(TMyObject.MethodAddress('Test'));   WriteLn(HexStr(TI)); Prints different results.

You cannot get typeinfo like that for methods.
You need to call GetPropInfo() from the typeinfo unit for published properties/methods.

How System.TypeInfo retrieve the typeinfo from my @TMyObject.Test address? It is done by the method address so?

No.

I took a look at the TRTTIWriter class and also in the VMT, but I don't know how the compiler handles this informations internally, so sorry it this question sounds nonsense. =P

You can check the reader class in classes unit.

Michael.

Reply by email

silvioprog
to Graeme Geldenhuys: You mean like what FPCUnit, DUnit or FPTest unit testing frameworks do? If so, just take…

On Fri, Jul 17, 2015 at 11:30 AM, Graeme Geldenhuys <

Show quoted text 12 lines
wrote: On 2015-07-17 15:13, silvioprog wrote: > I need to call a method dynamically by its name over string and pass some > parameters to it. You mean like what FPCUnit, DUnit or FPTest unit testing frameworks do? If so, just take a look at any of them to see how they call the test methods. Regards, - Graeme -

I know how FPCUnit does, but AFAIK it don't pass parameters for the triggered methods. =/

About DUnit, instead of take a loot at its code, I just use the System.Rtti.Invoke() feature.

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

silvioprog
to Michael Van Canneyt: You cannot get typeinfo like that for methods. You need to call GetPropInfo() from the…

On Fri, Jul 17, 2015 at 11:33 AM, Michael Van Canneyt <

Show quoted text 35 lines
wrote: On Fri, 17 Jul 2015, silvioprog wrote: Hello, > I need to call a method dynamically by its name over string and pass some > parameters to it. OK, I don't know if it is possibly in the current version > of the compiler, but I'm trying to do something > like this: > > TMethodInvoker.Invoke(VMyObj, ['Param 1', 'Param 2', 1, True]); > > I already get some paramerter informations using this code[1], that > prints all parameters and its types using RTTI. > > So I have a question: how can I get the method typedata looking for it in > the VMT? Please see this: > > WriteLn(HexStr(@TMyObject.Test)); > WriteLn(HexStr(TMyObject.MethodAddress('Test'))); > > Prints the same address. But: > > TI := TypeInfo(@TMyObject.Test); > WriteLn(HexStr(TI)); > TI := TypeInfo(TMyObject.MethodAddress('Test')); > WriteLn(HexStr(TI)); > > Prints different results. > You cannot get typeinfo like that for methods. You need to call GetPropInfo() from the typeinfo unit for published properties/methods.

=/

Yes, I'm just trying to avoiding an additional declaration of my procs, calling my methods using something like "published MyMethod(AParams: array of ...)" instead of calling events like "type TMyProc = procedure(AParams: array of ...) of object ...... published OnMyProc: TMyProc" etc.

Show quoted text 5 lines
How System.TypeInfo retrieve the typeinfo from my @TMyObject.Test address? > It is done by the method address so? > No.

=(

I took a look at the TRTTIWriter class and also in the VMT, but I don't

Show quoted text 5 lines
> know how the compiler handles this informations internally, so sorry it > this question sounds nonsense. =P > You can check the reader class in classes unit.

I'll do it again, thank you. =)

Silvio Clécio My public projects - github.com/silvioprog

Reply by email