且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IsAssignableFrom,IsInstanceOfType和是关键字,有什么区别?

更新时间:2022-04-25 08:30:08

您可以使用任何您拥有的信息。

You use whatever you have the information for.

您有一个要检查的实例和静态类型,请使用

If you have an instance and a static type you want to check against, use is.

具有静态类型,你只有一个类型对象,但你有一个要检查的实例,使用 IsInstanceOfType

If you don't have the static type, you just have a Type object, but you have an instance you want to check, use IsInstanceOfType.

如果您没有实例,而只想检查类型$ c $的理论实例之间的兼容性c>和另一个类型,请使用 IsAssignableFrom

If you don't have an instance and you just want to check the compatibility between a theoretical instance of a Type and another Type, use IsAssignableFrom.

但实际上似乎你只是重新实现作为operator (除了你的也可以用于不可为null的值类型,这通常不是一个大的限制)。

But really is seems like you are just re-implementing the as operator (except that yours would also work for non-nullable value types, which is usually not a big limitation).