如果我在 Delphi 中只有字符串名称,如何引用一个单元(在运行时)?

发布于 2024-11-08 07:51:15 字数 422 浏览 0 评论 0原文

我的类型具有相同名称的不同单元,并且单元名称位于字符串中。我需要访问该单元的特定类型。我该怎么做?

示例:

unit Unit1

type
   TFooType = (
      bar1,
      bar2
   );

然后,我有另一个单元

unit Unit2

type
   TFooType = (
      foo1,
      foo2,
      foo3
   );      

并且,在我的代码中的某处,我有一个字符串变量“UnitName”,其中包含值“Unit1”,我想通过该变量访问Unit1的“TFooType”类型。

我正在使用 Delphi 2007

抱歉我的英语不好。

提前致谢。

I have types in distincts units with the same name and I have the unit name in a string. I need to access the specific type of this unit. How do I do that?

Example:

unit Unit1

type
   TFooType = (
      bar1,
      bar2
   );

then, I have another unit

unit Unit2

type
   TFooType = (
      foo1,
      foo2,
      foo3
   );      

And, somewhere in my code I have the a string variable "UnitName" with the value 'Unit1' within it and I want to access the Unit1's "TFooType" type by the variable.

I'm using Delphi 2007

Sorry for my bad english.

Thanks in advance.

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

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

发布评论

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

评论(3

我做我的改变 2024-11-15 07:51:16

您无法选择在运行时包含哪些单位。单位是一个编译时概念。

此外,尽管您的两种类型具有相同的基本名称,但它们是完全不同的类型。在代码的其他地方,您不能拥有 TFooType 类型的变量并任意决定是否从这两个单元为其分配值。该变量只能保存一种类型的值。

您将不得不考虑其他方法来完成您的实际任务。我邀请您发布一个新问题来描述您的真正任务是什么。

You can't choose which units to include at run time. Units are a compile-time concept.

Furthermore, your two types, despite having the same base name, are completely distinct types. Elsewhere in your code, you cannot have a variable of type TFooType and arbitrarily decide whether to assign it values from both of those units. The variable can only hold values from one type.

You're going to have to think of some other way of accomplishing your real task. I invite you to post a new question describing what your real task is.

汹涌人海 2024-11-15 07:51:16

@Hrukai,就像乐高一样,你可以做很多事情,但有些东西并不是设计成这样使用的。

在我看来,您的最终目标是访问类型,而您的起点是变量名称。如果您将变量实现为类(OOP),您可以简单地执行 Obj.ClassName 来查找其类型...而且,如果您为实现选择了类,我预测这种需要(从变量访问类型)将从一开始就从未出现过。

抵制创造新模式的冲动,而利用阶级的力量。
http://www.delphibasics.co.uk/Article.asp?Name=OOExample

@Hrukai, Just like with lego, there's a lot you can do, but somethings just weren't designed to be used that way.

Sounds to me like your end goal is to access the type, and your starting point is a variable name. Had you implemented your variables as classes (OOP), you could simplly do Obj.ClassName to find its type... but also, had you chosen classes for your implementation, I predict this need (to access the type from the variable) would have never arisen in the first place.

Resist the urge to create a new pattern, and instead exploit the power of classes.
http://www.delphibasics.co.uk/Article.asp?Name=OOExample

似梦非梦 2024-11-15 07:51:16

你能做的最好的事情就是像 if name='Unit1' then T := Unit1.TFoo 等。但是你能用 T 做什么呢?由于不同单元的枚举类型不同,因此很难想象能够使用 T 进行任何操作。事实上,你会如何定义T?我唯一能想象到的可能就是返回类型信息,但我现在让我的想象力尽情发挥!

The best you could do would be something like if name='Unit1' then T := Unit1.TFoo etc. But what can you do with T anyway? Since the enumerated types from the different units are different, it's hard to imagine being able to anything with T. In fact how would you even define T? The only thing I could imagine would be possible would be to return the type info but I'm letting my imagination run wild now!

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