检查一个types的实例
用这个来检查c是否是TForm一个实例。 
 c.GetType().Name.CompareTo("TForm") == 0 
 除了使用string作为参数CompareTo()之外,还有更安全的方法吗? 
这里的不同答案有两个不同的含义。
如果你想检查一个实例是否是一个确切的types,那么
 if (c.GetType() == typeof(TForm)) 
是要走的路。
 如果你想知道c是TForm一个实例还是一个子类,那么使用is / as : 
 if (c is TForm) 
要么
 TForm form = c as TForm; if (form != null) 
在你的脑海里,你应该清楚这些行为究竟是你想要的。
 if(c is TFrom) { // Do Stuff } 
 或者如果您打算使用c作为TForm ,请使用以下示例: 
 var tForm = c as TForm; if(tForm != null) { // c is of type TForm } 
 第二个例子只需要检查c是否是TFormtypes的一次。 如果你检查c是TFormtypes的话,那么CLR会进行额外的检查。 这里有一个参考 。 
编辑:从乔恩Skeet窃取
 如果你想确保c是TForm而不是从TForminheritance的任何类,那么使用 
 if(c.GetType() == typeof(TForm)) { // Do stuff cause c is of type TForm and nothing else } 
是的,“是”关键字:
 if (c is TForm) { ... } 
查看MSDN上的详细信息: http : //msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx
检查对象是否与给定types兼容。 例如,可以确定一个对象是否与stringtypes兼容,如下所示:
此外,有些徒劳无功
 Type.IsAssignableFrom(Type c) 
“如果c和当前的Type代表相同的types,或者当前的Type位于c的inheritance层次结构中,或者当前的Type是c实现的接口,或者c是genericstypes参数,并且当前的Type代表了c的约束之一“
从这里: http : //msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx
尝试以下
 if (c is TForm) { ... } 
正如其他人所说,“是”关键字。 但是,如果你打算以后把它转换成这种types,例如。
 TForm t = (TForm)c; 
那么你应该使用“as”关键字。
 例如TForm t = c as TForm. 
然后你可以检查
 if(t != null) { // put TForm specific stuff here } 
不要结合,因为这是一个重复的检查。
如果你想使用c作为TForm,比其他答案更紧凑:
 if(c is TForm form){ form.DoStuff(); } 
要么
 c.getType() == typeOf(TForm) 
 bool isValid = c.GetType() == typeof(TForm) ? true : false; 
或者更简单
 bool isValid = c.GetType() == typeof(TForm);