1

如何将 tkEnumeration 作为 RTTI 参数传递给 Method.Invoke。

我努力了

TMyEnums  =  (tsEnum1, tsEnum2);


ParameterList : Array of TValue;

lTypeInfo : PTypeInfo;


lTypeInfo := TypeInfo(TMyEnums);

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

Method.Invoke(Object, ParameterList);

哪个失败了。Method.Invoke 可以采用 tkEnumeration 参数吗?即使它确实有效 - 在我的应用程序运行时我不知道参数的类型,所以我无法获得 lTypeInfo;

有没有办法从 TRttiParameter 获取 lTypeInfo?

我可以为 TRttiProperty 获取它,如下所示:

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

TRttiParameter 是否有等价物?

4

1 回答 1

4

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.

于 2013-08-17T14:51:14.813 回答