测试类引用(元类)变量中的类是否为 TMyClass
我想知道从类引用创建的对象是否是特定类或其任何后代的实例。
换句话说,我想要一个布尔表达式,例如
var is TMyClass
but 其中 var 被替换为涉及类引用变量的表达式。这听起来很容易,但让我完全难住了。
我可以创建一个实例 var := classRefVar.Create,测试它,然后销毁它,但这是一个巨大的开销。
奇怪的是,编译器不会让我拥有
classRefVar(nil) is TMyClass
,但对看似等效的语法感到满意,但毫无用处。
TMyClass(nil) is TMyClass
显然,该表达式
classRefVar = TMyClass
不好,因为 classRefVar 可能引用 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 might refer to a descendent of TMyClass.
I'm expecting to kick myself when I see the answer...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的:
Easy: