检查实例是否属于某种类型

时间:2022-04-11 17:37:48

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

用它来检查c是否是TForm的一个实例。

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

Is there a more type safe way to do it besides using a string as a param to CompareTo()?

除了使用字符串作为CompareTo()的参数之外,还有更安全的类型吗?

9 个解决方案

#1


318  

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))

is the way to go.

是要走的路。

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

如果你想知道c是TForm的实例还是子类,那么使用/ as:

if (c is TForm)

or

要么

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

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

值得在脑海中清楚地了解您真正想要的这些行为。

#2


29  

if(c is TFrom)
{
   // Do Stuff
}

or if you plan on using c as a TForm, use the following example:

或者如果您打算将c用作TForm,请使用以下示例:

var tForm = c as TForm;
if(tForm != null)
{
   // c is of type TForm
}

The second example only needs to check to see if c is of type TForm once. Whereis if you check if see if c is of type TForm then cast it, the CLR undergoes an extra check. Here is a reference.

第二个例子只需检查c是否为TForm类型一次。如果你检查c是否是TForm类型然后施放它,那么CLR会经过额外的检查。这是一个参考。

Edit: Stolen from Jon Skeet

编辑:Jon Skeet被盗

If you want to make sure c is of TForm and not any class inheriting from TForm, then use

如果你想确保c是TForm而不是任何继承自TForm的类,那么使用

if(c.GetType() == typeof(TForm))
{
   // Do stuff cause c is of type TForm and nothing else
}

#3


11  

Yes, the "is" keyword:

是的,“是”关键字:

if (c is TForm)
{
    ...
}

See details on MSDN: http://msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx

有关MSDN的详细信息,请访问:http://msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx

Checks if an object is compatible with a given type. For example, it can be determined if an object is compatible with the string type like this:

检查对象是否与给定类型兼容。例如,可以确定对象是否与字符串类型兼容,如下所示:

#4


7  

Also, somewhat in the same vain

而且,有点同样徒劳无功

Type.IsAssignableFrom(Type c)

"True if c and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c implements, or if c is a generic type parameter and the current Type represents one of the constraints of c."

“如果c和当前Type表示相同类型,或者当前Type在c的继承层次结构中,或者当前Type是c实现的接口,或者c是泛型类型参数和当前类型,则为真代表c的约束之一。“

From here: http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

从这里:http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

#5


6  

A little more compact than the other answers if you want to use c as a TForm:

如果你想使用c作为TForm,比其他答案更紧凑:

if(c is TForm form){
    form.DoStuff();
}

#6


3  

Try the following

请尝试以下方法

if (c is TForm) { 
 ...
}

#7


2  

As others have mentioned, the "is" keyword. However, if you're going to later cast it to that type, eg.

正如其他人提到的那样,“是”关键字。但是,如果您以后要将其转换为该类型,例如。

TForm t = (TForm)c;

Then you should use the "as" keyword.

然后你应该使用“as”关键字。

e.g. TForm t = c as TForm.

例如TForm t = c作为TForm。

Then you can check

然后你可以检查

if(t != null)
{
 // put TForm specific stuff here
}

Don't combine as with is because it's a duplicate check.

不要合并因为它是重复检查。

#8


0  

Or

要么

c.getType() == typeOf(TForm)

#9


-1  

bool isValid = c.GetType() == typeof(TForm) ? true : false;

or simpler

或者更简单

bool isValid = c.GetType() == typeof(TForm);

#1


318  

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))

is the way to go.

是要走的路。

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

如果你想知道c是TForm的实例还是子类,那么使用/ as:

if (c is TForm)

or

要么

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

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

值得在脑海中清楚地了解您真正想要的这些行为。

#2


29  

if(c is TFrom)
{
   // Do Stuff
}

or if you plan on using c as a TForm, use the following example:

或者如果您打算将c用作TForm,请使用以下示例:

var tForm = c as TForm;
if(tForm != null)
{
   // c is of type TForm
}

The second example only needs to check to see if c is of type TForm once. Whereis if you check if see if c is of type TForm then cast it, the CLR undergoes an extra check. Here is a reference.

第二个例子只需检查c是否为TForm类型一次。如果你检查c是否是TForm类型然后施放它,那么CLR会经过额外的检查。这是一个参考。

Edit: Stolen from Jon Skeet

编辑:Jon Skeet被盗

If you want to make sure c is of TForm and not any class inheriting from TForm, then use

如果你想确保c是TForm而不是任何继承自TForm的类,那么使用

if(c.GetType() == typeof(TForm))
{
   // Do stuff cause c is of type TForm and nothing else
}

#3


11  

Yes, the "is" keyword:

是的,“是”关键字:

if (c is TForm)
{
    ...
}

See details on MSDN: http://msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx

有关MSDN的详细信息,请访问:http://msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx

Checks if an object is compatible with a given type. For example, it can be determined if an object is compatible with the string type like this:

检查对象是否与给定类型兼容。例如,可以确定对象是否与字符串类型兼容,如下所示:

#4


7  

Also, somewhat in the same vain

而且,有点同样徒劳无功

Type.IsAssignableFrom(Type c)

"True if c and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c implements, or if c is a generic type parameter and the current Type represents one of the constraints of c."

“如果c和当前Type表示相同类型,或者当前Type在c的继承层次结构中,或者当前Type是c实现的接口,或者c是泛型类型参数和当前类型,则为真代表c的约束之一。“

From here: http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

从这里:http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

#5


6  

A little more compact than the other answers if you want to use c as a TForm:

如果你想使用c作为TForm,比其他答案更紧凑:

if(c is TForm form){
    form.DoStuff();
}

#6


3  

Try the following

请尝试以下方法

if (c is TForm) { 
 ...
}

#7


2  

As others have mentioned, the "is" keyword. However, if you're going to later cast it to that type, eg.

正如其他人提到的那样,“是”关键字。但是,如果您以后要将其转换为该类型,例如。

TForm t = (TForm)c;

Then you should use the "as" keyword.

然后你应该使用“as”关键字。

e.g. TForm t = c as TForm.

例如TForm t = c作为TForm。

Then you can check

然后你可以检查

if(t != null)
{
 // put TForm specific stuff here
}

Don't combine as with is because it's a duplicate check.

不要合并因为它是重复检查。

#8


0  

Or

要么

c.getType() == typeOf(TForm)

#9


-1  

bool isValid = c.GetType() == typeof(TForm) ? true : false;

or simpler

或者更简单

bool isValid = c.GetType() == typeof(TForm);