什么是Java的isInstance()等效的C#?

时间:2022-09-01 09:36:32

I know of is and as for instanceof, but what about the reflective isInstance() method?

我知道是和例如,但反射isInstance()方法怎么样?

5 个解决方案

#1


50  

The equivalent of Java’s obj.getClass().isInstance(otherObj) in C# is as follows:

C#中Java的obj.getClass()。isInstance(otherObj)的等价如下:

bool result = obj.GetType().IsAssignableFrom(otherObj.GetType());

Note that while both Java and C# work on the runtime type object (Java java.lang.Class ≣ C# System.Type) of an obj (via .getClass() vs .getType()), Java’s isInstance takes an object as its argument, whereas C#’s IsAssignableFrom expects another System.Type object.

请注意,虽然Java和C#都使用obj的运行时类型对象(Java java.lang.Class≣C#System.Type)(通过.getClass()vs .getType()),但Java的isInstance将对象作为其参数而C#的IsAssignableFrom需要另一个System.Type对象。

#2


178  

bool result = (obj is MyClass); // Better than using 'as'

#3


39  

Depends, use is if you don't want to use the result of the cast and use as if you do. You hardly ever want to write:

取决于,如果您不想使用演员表的结果并使用,就像使用一样。你几乎不想写:

if(foo is Bar) {
    return (Bar)foo;
}

Instead of:

var bar = foo as Bar;
if(bar != null) {
    return bar;
}

#4


2  

just off the top of my head, you could also do:

就在我的头顶,你也可以这样做:

bool result = ((obj as MyClass) != null)

Not sure which would perform better. I'll leave it up to someone else to benchmark :)

不确定哪个会表现更好。我会把它留给别人来测试:)

#5


2  

Below code can be alternative to IsAssignableFrom.

下面的代码可以替代IsAssignableFrom。

parentObject.GetType().IsInstanceOfType(inheritedObject)

See Type.IsInstanceOfType description in MSDN.

请参阅MSDN中的Type.IsInstanceOfType描述。

#1


50  

The equivalent of Java’s obj.getClass().isInstance(otherObj) in C# is as follows:

C#中Java的obj.getClass()。isInstance(otherObj)的等价如下:

bool result = obj.GetType().IsAssignableFrom(otherObj.GetType());

Note that while both Java and C# work on the runtime type object (Java java.lang.Class ≣ C# System.Type) of an obj (via .getClass() vs .getType()), Java’s isInstance takes an object as its argument, whereas C#’s IsAssignableFrom expects another System.Type object.

请注意,虽然Java和C#都使用obj的运行时类型对象(Java java.lang.Class≣C#System.Type)(通过.getClass()vs .getType()),但Java的isInstance将对象作为其参数而C#的IsAssignableFrom需要另一个System.Type对象。

#2


178  

bool result = (obj is MyClass); // Better than using 'as'

#3


39  

Depends, use is if you don't want to use the result of the cast and use as if you do. You hardly ever want to write:

取决于,如果您不想使用演员表的结果并使用,就像使用一样。你几乎不想写:

if(foo is Bar) {
    return (Bar)foo;
}

Instead of:

var bar = foo as Bar;
if(bar != null) {
    return bar;
}

#4


2  

just off the top of my head, you could also do:

就在我的头顶,你也可以这样做:

bool result = ((obj as MyClass) != null)

Not sure which would perform better. I'll leave it up to someone else to benchmark :)

不确定哪个会表现更好。我会把它留给别人来测试:)

#5


2  

Below code can be alternative to IsAssignableFrom.

下面的代码可以替代IsAssignableFrom。

parentObject.GetType().IsInstanceOfType(inheritedObject)

See Type.IsInstanceOfType description in MSDN.

请参阅MSDN中的Type.IsInstanceOfType描述。