Delphi 2010:TRTTICConstructor 发生了什么?
我有两个问题(其中至少一个是关于 D2010 中的 RTTI 和动态实例)
- 我正在阅读似乎是 巴里·凯利 (Barry Kelly) 的会议演讲,可在第 11 页找到。 13 一些看起来非常有趣的东西:
TRTTIConstructor.Invoke
。在相邻的要点中,人们发现“动态构造实例而不需要虚拟构造函数和元类”。这似乎是一个很棒的功能(顺便说一句,这正是我所需要的)!但是,当我查看 D2010 文档 (ms-help://embarcadero.rs2010/vcl/Rtti.html) 时,我找不到它。被撤销了吗? - 假设类名存储在字符串中,创建类实例的最小方法是什么?
I've got two questions (of which at least one is regarding RTTI in D2010 and dynamic instancing)
- 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? - What is the minimal way of creating an instance of a class, provided the class name is stored in a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 TRttiMethod 已吸收了该功能。它具有 IsConstructor、IsDestructor 和 IsClassMethod 属性,因此它可以用于“特殊”类型的方法以及普通方法。
至于问题 2,请尝试这样的操作:
这会找到名为
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:
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 callInvoke
on the result.虽然您可以调用 .GetMethod() 来获取构造函数,但您也可以执行以下操作来构造不带构造函数参数的对象实例。
如果知道基类型,您可以根据需要对类进行类型转换以传递参数。
下面是创建组件的示例
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.
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);