Delphi 2010:TRTTICConstructor 发生了什么?

发布于 2024-08-26 05:37:12 字数 465 浏览 9 评论 0原文

我有两个问题(其中至少一个是关于 D2010 中的 RTTI 和动态实例)

  1. 我正在阅读似乎是 巴里·凯利 (Barry Kelly) 的会议演讲,可在第 11 页找到。 13 一些看起来非常有趣的东西:TRTTIConstructor.Invoke。在相邻的要点中,人们发现“动态构造实例而不需要虚拟构造函数和元类”。这似乎是一个很棒的功能(顺便说一句,这正是我所需要的)!但是,当我查看 D2010 文档 (ms-help://embarcadero.rs2010/vcl/Rtti.html) 时,我找不到它。被撤销了吗?
  2. 假设类名存储在字符串中,创建类实例的最小方法是什么?

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

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失去的东西太少 2024-09-02 05:37:12

我认为 TRttiMethod 已吸收了该功能。它具有 IsConstructor、IsDestructor 和 IsClassMethod 属性,因此它可以用于“特殊”类型的方法以及普通方法。

至于问题 2,请尝试这样的操作:

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;

这会找到名为 Create 且不带参数的最高构造函数。如果您知道要查找什么,则可以修改它以查找具有其他签名的其他构造函数。然后只需对结果调用 Invoke 即可。

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.

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.

执笔绘流年 2024-09-02 05:37:12

虽然您可以调用 .GetMethod() 来获取构造函数,但您也可以执行以下操作来构造不带构造函数参数的对象实例。

function CreateInstance(aType : TRttiType) : TObject;
begin
  // Option #1
  result := aType.AsInstance.MetaclassType.Create;
  // Option #2
  result := aType.GetMethod('Create').Invoke(aType.AsInstance.MetaclassType,[]);
end;

如果知道基类型,您可以根据需要对类进行类型转换以传递参数。
下面是创建组件的示例

result := TComponentClass(aType.AsInstance.MetaClassType).Create(OwnerValue);

Although you can call .GetMethod() to get the constructor you can also do the following to construct instances of objects with no parameters for the constructor.

function CreateInstance(aType : TRttiType) : TObject;
begin
  // Option #1
  result := aType.AsInstance.MetaclassType.Create;
  // Option #2
  result := aType.GetMethod('Create').Invoke(aType.AsInstance.MetaclassType,[]);
end;

If know the base type you can type cast the class to pass the parameters if you wish.
Here is an example of creating a Component

result := TComponentClass(aType.AsInstance.MetaClassType).Create(OwnerValue);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文