相关疑难解决方法(0)

如何检查类型是子类型还是对象类型?

要检查类型是否是C#中另一种类型的子类,很容易:

typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true
Run Code Online (Sandbox Code Playgroud)

但是,这将失败:

typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false
Run Code Online (Sandbox Code Playgroud)

有没有办法检查类型是否是基类本身的子类OR,而不使用OR运算符或使用扩展方法?

c# reflection types subclass

322
推荐指数
3
解决办法
16万
查看次数

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

用它来检查是否c是实例TForm.

c.GetType().Name.CompareTo("TForm") == 0
Run Code Online (Sandbox Code Playgroud)

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

.net c#

165
推荐指数
5
解决办法
18万
查看次数

如何检查一个类是否继承了另一个类而没有实例化它?

假设我有一个看起来像这样的类:

class Derived : // some inheritance stuff here
{
}
Run Code Online (Sandbox Code Playgroud)

我想在我的代码中检查这样的内容:

Derived is SomeType;
Run Code Online (Sandbox Code Playgroud)

但看起来像is运算符需要Derived是Dervied类型的变量,而不是Derived本身.我不想创建Derived类型的对象.
如何在SomeType不实例化的情况下确保Derived继承?

PS如果它有帮助,我想要一些where关键字与泛型有关的东西.
编辑:
类似于这个答案,但它正在检查一个对象.我想查看课程本身.

c# inheritance types

130
推荐指数
2
解决办法
9万
查看次数

标签 统计

c# ×3

types ×2

.net ×1

inheritance ×1

reflection ×1

subclass ×1