Question

How can I pass a tkEnumeration as an RTTI Parameter to Method.Invoke.

I have tried

TMyEnums  =  (tsEnum1, tsEnum2);


ParameterList : Array of TValue;

lTypeInfo : PTypeInfo;


lTypeInfo := TypeInfo(TMyEnums);

ParameterList[0] := TValue.FromOrdinal(lTypeInfo, Integer(tsEnum1)); 

Method.Invoke(Object, ParameterList);

Which fails. Can Method.Invoke take a tkEnumeration parameter? Even if it did work - in my application at run time I do not know the type of the parameter so I can't obtain lTypeInfo;

Is there a way to obtain lTypeInfo from TRttiParameter?

I can get it for TRttiProperty as follows:

lTypeInfo := RTTIProperties[i].GetValue(SourceObject).TypeInfo

Is there an equivalent for TRttiParameter?

Was it helpful?

Solution

You need to use reflection to find the type of the parameter:

  1. Call GetParameters on the method (TRttiMethod instance) to get an array of parameters. That is an array of TRttiParameter.
  2. On a TRttiParameter instance, use ParamType to obtain a TRttiType instance that describes the type.
  3. Use the Handle property of the TRttiType instance to get at the type info.
  4. Use that type info when calling TValue.FromOrdinal to make your TValue instance.
  5. Invoke your method.
  6. Profit!

I don't have a compiler here so I won't attempt to write code for this. Hopefully the outline above is enough for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top