Delphi 2010:TRTTIConstructor发生了什么事? [英] Delphi 2010: whatever happened to TRTTIConstructor?

查看:183
本文介绍了Delphi 2010:TRTTIConstructor发生了什么事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题(其中至少有一个是在D2010和动态实例中讨论RTTI)

I've got two questions (of which at least one is regarding RTTI in D2010 and dynamic instancing)


  1. 我在读什么似乎是Barry Kelly的大会谈话的烙印,以及在p。 13看起来很有趣的东西: TRTTIConstructor.Invoke 。在相邻的项目符号点中,可以找到动态构造实例而不需要虚拟构造函数和元类。这似乎是一个伟大的功能(正是我需要的,btw)!但是,当我查看D2010文档(ms-help://embarcadero.rs2010/vcl/Rtti.html)时,我找不到它。是否被撤销?

  2. 创建类的实例的最小方式是什么,只要类名存储在字符串中?

  1. I was reading what appears to be the foils for a conference talk by Barry Kelly, and found on p. 13 something that looked really interesting: TRTTIConstructor.Invoke. In an adjacent bullet point, one finds "Dynamically construct instances without needing virtual constructors and metaclasses". This seems like a great feature (and exactly what I need, btw)! However, when I look in the D2010 docs (ms-help://embarcadero.rs2010/vcl/Rtti.html), I can't find it. Did it get revoked?
  2. What is the minimal way of creating an instance of a class, provided the class name is stored in a string?


推荐答案

我认为功能已被TRttiMethod所吸收。它具有IsConstructor,IsDestructor和IsClassMethod属性,因此它可以用于特殊类型的方法以及常规方法。

I think that functionality has been absorbed into TRttiMethod. It has IsConstructor, IsDestructor and IsClassMethod properties so that it can be used for "special" types of methods as well as normal ones.

对于问题2,尝试类似这个:

As for question 2, try something like this:

function GetConstructor(val: TRttiInstanceType): TRttiMethod;
var
   method: TRttiMethod;
begin
   for method in val.GetMethods('Create') do
   begin
      if (method.IsConstructor) and (length(method.GetParameters) = 0) then
         exit(method);
   end;
   raise EInsufficientRTTI.CreateFmt('No simple constructor available for class %s ',
                                     [val.MetaclassType.ClassName]);
end;

找到名为的最高构造函数创建没有参数。您可以修改它,以查找具有其他签名的其他构造函数,如果您知道要查找的内容。然后在结果中调用调用

This finds the highest constructor called Create that takes no parameters. You can modify it to look for other constructors with other signatures, if you know what you're looking for. Then just call Invoke on the result.

这篇关于Delphi 2010:TRTTIConstructor发生了什么事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆