jpf*_*ius 2 delphi generics casting delphi-2010
我目前正在考虑一个场景,我想要一个调用Free其项目的泛型类,如果它们是一个对象类型.所以我尝试了以下方法:
if (PTypeInfo (TypeInfo (T)).Kind = tkClass) then
begin
RttiType := RttiContext.GetType (TypeInfo (T));
FreeMethod := RttiType.GetMethod ('Free');
if (FreeMethod <> nil) then
FreeMethod.Invoke (FInstance, []);
end;
Run Code Online (Sandbox Code Playgroud)
不幸的是,最后一行没有编译,这并不奇怪,因为类型不匹配.问题是:我可以编译吗?我尝试了转换Pointer然后转到TObject但是这给了我一个无效的类型转换错误.有没有办法FInstance : T进入TObject我可以传递给的参考Invoke?
或者有没有其他方法来实现我想要的?请注意,重点是获取此类中的所有内容,因此我不想创建TObjectMyClass只接受对象的内容.
谢谢你的帮助.
这不够吗?
if PTypeInfo(TypeInfo(T))^.Kind = tkClass then
TObject(FInstance).Free;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1929 次 |
| 最近记录: |