检查一个类型的实例 [英] check if instance of a type

查看:100
本文介绍了检查一个类型的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此检查 C TForm的的实例。

Using this to check if c is a instance of TForm.

c.GetType().Name.CompareTo("TForm") == 0

有没有更类型安全的方式来做到这一点,除了使用一个字符串作为参数,以的CompareTo()

推荐答案

不同的答案在这里有两种不同的含义。

The different answers here have two different meanings.

如果您要检查一个实例是否是的确切类型的话

If you want to check whether an instance is of an exact type then

if (c.GetType() == typeof(TForm))

是要走的路。

如果你想知道是否 C TForm的的一个实例的或子类然后用 /

If you want to know whether c is an instance of TForm or a subclass then use is/as:

if (c is TForm)

TForm form = c as TForm;
if (form != null)

这是值得的在你的心中对这些行为,你真的想清楚了。

It's worth being clear in your mind about which of these behaviour you actually want.

这篇关于检查一个类型的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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