与Java的isInstance()等效的C#是什么? [英] What is the C# equivalent to Java's isInstance()?
本文介绍了与Java的isInstance()等效的C#是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于 instanceof
,我知道是
和 as
,但是反射 isInstance()方法?
I know of is
and as
for instanceof
, but what about the reflective isInstance() method?
推荐答案
与Java的 obj等效。 C#中的getClass()。isInstance(otherObj)
如下:
bool result = obj.GetType().IsAssignableFrom(otherObj.GetType());
请注意,尽管Java和C#均可在运行时类型对象上运行(Java java.lang.Class
≣ obj
的C# System.Type
)(通过 .getClass()
与 .getType()
),Java的 isInstance
将对象作为参数,而C#的 IsAssignableFrom
期望另一个 System.Type
对象。
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的isInstance()等效的C#是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文