如何从 Delphi 6 对象获取类以分配给元类变量?

发布于 2024-12-10 09:58:26 字数 1230 浏览 0 评论 0原文

我有一个 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在这个线程中的回复所示:

如何从类引用创建 Delphi 对象并确保构造函数执行?

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 技术交流群。

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

发布评论

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

评论(1

秋意浓 2024-12-17 09:58:26

我相信您正在寻找 TObject.ClassType。像这样使用它:

procedure setClientClass(theClient: TClient);
begin
  FClientObj := theClient;
  FClientClass := TClientClass(theClient.ClassType);
end;

注释1:我假设您的意思是FClientClass:TClientClass而不是FClientClass:TClientClass的类

注2:我只是在脑子里编了这个;我希望它可以在真正的编译器上运行。

I believe you are looking for TObject.ClassType. Use it like this:

procedure setClientClass(theClient: TClient);
begin
  FClientObj := theClient;
  FClientClass := TClientClass(theClient.ClassType);
end;

Note 1: I'm assuming that you mean FClientClass: TClientClass rather than FClientClass: class of TClientClass.

Note 2: I've only compiled this in my head; I hope it works on a real compiler.

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