在C#中,"is"关键字的等价物是什么,但使用的是Type对象

ilc*_*ero 4 c# introspection

我想这是一个简单的问题,但在Type类的文档中,他们只讨论了GetInterfaces方法中的接口.

ie typeof(ChildClass).XXX(typeof(ParentClass)

Mar*_*ell 14

这取决于你需要什么; IsAssignableFrom,也许:

bool stringIsObj = typeof(object).IsAssignableFrom(typeof(string));
Run Code Online (Sandbox Code Playgroud)

或者IsSubclassOf:

bool stringIsObj = typeof(string).IsSubclassOf(typeof(object));
Run Code Online (Sandbox Code Playgroud)

  • 当然; 接口将以不同的方式工作(它们不会是子类) - 并且Foo可以从Foo分配,但是Foo不是Foo的子类(IsSubclassOf是**严格的**子类) (4认同)