类引用作为属性 [英] Class Reference as Property

查看:145
本文介绍了类引用作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google对于这类搜索毫无用处,因为您会获得数亿个结果,而这些结果绝对与特定问题无关.

Google is useless for these sorts of searches, because you get hundreds of millions of results absolutely none of which relate to the specific question.

问题很简单:

  • 是否可以在Delphi中具有类引用属性?
  • 如果是,怎么办?

这就是我尝试过的...

Here's what I've tried...

type

  TMyObject = class
    // ...
  end;

  TMyObjectClass = class of TMyObject

  TMyObjectA = class(TMyObject)
    // specifics here
  end;

  TMyObjectB  =class(TMyObject)
    // specifics here
  end;

  TMyComponent = class(TComponent)
  private
    FObjectType: TMyObjectClass;
  published
    property ObjectType: TMyObjectClass read FObjectType write FObjectType;
  end;

上面的代码可以很好地编译,但是对象检查器根本不显示ObjectType属性.

The above code compiles fine, however the Object Inspector does not show the ObjectType property at all.

我的目的(如果您还没有猜到)是为了使它能够从特定的基类中选择一个类的后代,以使相同的组件以不同的方式工作.

My objective here (if you haven't already guessed) is to make it so that I can select a class descendant from a specific base class, to make the same component behave in a different way.

我想这样做,以便组件不需要直接了解子类(它必须是完全模块化的).

I want to do it this way so that the component doesn't need to know about the sub-classes directly (it needs to be fully modular).

让我澄清一下:我不能使用枚举在子类类型之间进行选择,因为组件无法直接链接到子类类型(在这种情况下是可能的)

无论如何...预先感谢!

Anyway... thanks in advance!

推荐答案

您可以找到所有源自特定基类的类:

You can find all classes that descend from a particular base class: Delphi: At runtime find classes that descend from a given base class? and make this a special property with list of values using TPropertyEditor.

这篇关于类引用作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆