Delphi 获取类

发布于 2024-12-09 23:04:32 字数 1037 浏览 2 评论 0原文

我的问题是如何访问其他单元中的课程?举个例子:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  System,
  StrUtils,
  Math,
  TypInfo,
  Data in 'Data.pas';

var
  Str, name, value                      : string;
  List, tmpList                         : TStringList;
  i                                     : Integer;
  Obj                                   : TObject;
  CRef                                  : TPersistentClass;
  d                                     : TData;
begin
  d := TData(GetClass('Data.TData').Create);
  Writeln(Format('%s', [d.Name]));
  Readln;
  Readln;
end.

数据单元:

unit Data;

interface
 uses
  SysUtils,
  Classes;
type
  TData = class(TObject)
    FName : string;
  published
    property Name : string read FName write FName;
  end;
type
  TIn = class(TObject)
    FName : string;
  published
    property Name : string read FName write FName;
  end;
implementation

end.

问题是 GetClass 方法总是返回 nil。 我知道存在这样的问题,但它们对我没有帮助。

提前致谢!

My question is how to access a class which is in an other unit? For an example:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  System,
  StrUtils,
  Math,
  TypInfo,
  Data in 'Data.pas';

var
  Str, name, value                      : string;
  List, tmpList                         : TStringList;
  i                                     : Integer;
  Obj                                   : TObject;
  CRef                                  : TPersistentClass;
  d                                     : TData;
begin
  d := TData(GetClass('Data.TData').Create);
  Writeln(Format('%s', [d.Name]));
  Readln;
  Readln;
end.

And the Data unit :

unit Data;

interface
 uses
  SysUtils,
  Classes;
type
  TData = class(TObject)
    FName : string;
  published
    property Name : string read FName write FName;
  end;
type
  TIn = class(TObject)
    FName : string;
  published
    property Name : string read FName write FName;
  end;
implementation

end.

The problem is that the method GetClass return me always nil.
I know that there is a questions like this one but they doesn't helped me.

Thanks in advance!

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

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

发布评论

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

评论(4

转角预定愛 2024-12-16 23:04:32

如果您使用的是较新的 Delphi 版本之一,则可以使用 RTTI 单元。

uses RTTI;
..
  var
    R : TRttiContext;
  begin
    R.FindType('Data.TData')
...

在您的示例中,TIn 不是内部类,但也可以像这样访问内部类:

R.FindType('Data.TData.TIn')

If you are using one of the later Delphi versions you can use the RTTI unit.

uses RTTI;
..
  var
    R : TRttiContext;
  begin
    R.FindType('Data.TData')
...

In your example TIn is not a inner class, but inner classes can also be accessed like this:

R.FindType('Data.TData.TIn')
雨巷深深 2024-12-16 23:04:32

鉴于d被定义为TData,你不能简单地使用d := TData.Create()吗?

无论如何,如果你想从它的名称创建类,你必须(看看此链接)在使用 GetClass 之前调用 RegisterClass,否则该类将无法被识别,并且您会得到一个

Given that d is defined as TData, can't you simply use d := TData.Create()?

Anyway if you want to create class from its name, you have to (look this link) call RegisterClass before using GetClass or that class won't be recognized and you get a nil.

遗失的美好 2024-12-16 23:04:32

你没有注册课程。您需要调用RegisterClass

如果我只有字符串中的名称,如何注册课程?

通常,您会在声明类的初始化部分单元中调用 RegisterClass

当您调用RegisterClass时,您会发现该类需要从TPercient派生:

procedure RegisterClass(AClass: TPersistentClass);

You didn't register the class. You need to call RegisterClass.

How can I register class if I have only the name in string?

Typically you would place a call to RegisterClass in the initialization section unit that declares the class.

When you come to call RegisterClass you will discover that the class needs to derived from TPersistent:

procedure RegisterClass(AClass: TPersistentClass);
情定在深秋 2024-12-16 23:04:32

请阅读 GetClass 文档。它仅适用于注册的持久类。因此,为了使用它,您必须更改的第一件事是从 TPersistent 继承,即

type
  TData = class(TPersistent)
    FName : string;
  published
    property Name : string read FName write FName;
  end;

然后您必须确保该类已注册,即您必须调用 RegisterClasses,可能在单元的初始化部分

initialization
  RegisterClasses([TData]);

Please read the GetClass documentation. It only works with registered persistent classes. So The first thing you must change in order to use it is descend from TPersistent, ie

type
  TData = class(TPersistent)
    FName : string;
  published
    property Name : string read FName write FName;
  end;

and then you have to make sure that the class is registred, ie you have to call RegisterClasses, perhaps in the initialization section of the unit

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