开发者

Test if class from a class reference (metaclass) variable is TMyClass

I want to know whether the object that would be created from a class reference is an instance of a particular class or any of its descendants.

In other words, I want a Boolean expression such as

var is TMyClass

but where var is replaced with an expression involving a class reference variable. It sounds easy but has me completely stumped.

I could create an instance var := classRefVar.Create, test it, and then destroy it, but that's a huge overhead.

Oddly, the compiler won't let me have

classRefVar(nil) is TMyClass

but is happy with the seemingly equivalent syntactically, but useless

TMyClass(nil) is TMyClass

Obviously, the expression

classRefVar = TMyClass

is no good because the classRefVar m开发者_Python百科ight refer to a descendent of TMyClass.

I'm expecting to kick myself when I see the answer...


Easy:

ClassRefVar.InheritsFrom(TMyClass)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜