如何从 Delphi 6 对象获取类以分配给元类变量?
我有一个 Delphi 6 元类变量,它为设计时服务器组件的组件属性提供服务:
type
TClientClass = class of TClient;
...
FClientClass: class of TClientClass;
FClientObj: TClient;
我有另一个设计时组件,它是客户端组件。在设计时,我通过 IDE 的属性编辑器使用服务器的“客户端”属性将客户端组件的具体实例分配给服务器组件,并将其拖放到同一表单上。但是,我还想将 FClientClass 变量分配给我通过“client”属性分配给 FClientObj 的具体组件的基础类。这样服务器组件就可以在运行时使用 FClientClass.Create 创建客户端组件的新实例。
当我通过服务器组件的属性设置器设置 FClientObj 数据成员时,我无法弄清楚如何将“客户端”对象的基础类分配给 FClientClass 数据成员:
procedure setClientClass(theClient: TClient);
begin
// Assign the "client" property. This works.
FClientObj := theClient;
// Assign the class of the "client" object to the Metaclass variable.
// THIS DOESN'T WORK: incompatible types error from the compiler.
FClientClass := theClient;
end;
我很确定问题是我正在尝试将 TClient 的对象分配给 TClient 变量的元类。我只是不知道进行分配的正确语法。我不想只是这样做:
FClientClass := TClient;
因为我想允许将来分配可能是 TClient 后代的具体组件。
正确执行赋值的语法是什么?我希望它比使用 RTTI 库做一些复杂的事情更简单,就像Malte在这个线程中的回复所示:
I have a Delphi 6 Metaclass variable that services a component property for a design time Server component:
type
TClientClass = class of TClient;
...
FClientClass: class of TClientClass;
FClientObj: TClient;
I have another design-time component that is a Client component. At design time I assign a concrete instance of the Client component, dropped on to the same Form, to the Server component using the Server's "client" property via the IDE's Property Editor. However, I also want to assign the FClientClass variable the underlying class of the concrete component I am assigning to FClientObj via the "client" property. This is so the Server component can create new instances of the Client component at run time, using FClientClass.Create.
I can't figure out how to assign the underlying class of the "client" object to the FClientClass data member when I set the FClientObj data member via the Server component's property setter:
procedure setClientClass(theClient: TClient);
begin
// Assign the "client" property. This works.
FClientObj := theClient;
// Assign the class of the "client" object to the Metaclass variable.
// THIS DOESN'T WORK: incompatible types error from the compiler.
FClientClass := theClient;
end;
I'm pretty sure the problem is that I am trying to assign an object of TClient to a Metaclass of TClient variable. I just don't know the correct syntax to make the assignment. I don't want to just do:
FClientClass := TClient;
because I want to allow for the assignation of concrete components in the future that may be descendants of TClient.
What is the syntax for doing the assignment correctly? I'm hoping it is simpler than doing something complex with the RTTI library like as Malte's reply in this thread indicates:
How can I create an Delphi object from a class reference and ensure constructor execution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您正在寻找
TObject.ClassType
。像这样使用它:注释1:我假设您的意思是
FClientClass:TClientClass
而不是FClientClass:TClientClass的类
。注2:我只是在脑子里编了这个;我希望它可以在真正的编译器上运行。
I believe you are looking for
TObject.ClassType
. Use it like this:Note 1: I'm assuming that you mean
FClientClass: TClientClass
rather thanFClientClass: class of TClientClass
.Note 2: I've only compiled this in my head; I hope it works on a real compiler.