c# - 检查 System.Type 是否是给定类的后代的最佳方法

标签 c# .net reflection type-systems system.type

考虑以下代码:

public class A 
{
}  

public class B : A 
{
}  

public class C : B 
{
}  

class D  
{  
    public static bool IsDescendantOf(this System.Type thisType, System.Type thatType)  
    {  
        /// ??? 
    } 

    void Main()
    {
        A cValue = new C();
        C.GetType().IsDescendantOf(cValue.GetType());
    }
}

实现 IsDescendantOf 的最佳方法是什么?

最佳答案

Type.IsSubclassOf()判断当前Type表示的类是否派生自指定Type表示的类。

关于c# - 检查 System.Type 是否是给定类的后代的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1433750/

相关文章:

c# - 如何将控制台输出转换为字符串?

c# - 在 WPF 中打开关联文件,例如 'notepad++ tab features'

c# - 此版本的存储模拟器不支持此请求的 REST 版本

c# - 使用 LINQ 在对象图中的所有属性中查找关键字

java - 如何在 Java 中通过反射传递枚举参数

c# - 如何制作在任务栏中的程序windows-CE

c# - ASP.Net 身份检查多个角色以获取访问权限

.net - 在运行时修改 Entity Framework 模型

c# - ReportViewer 错误 - 报告的定义“无效。无法加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectMode

pointers - 如何在不指定类型的情况下修改函数输入参数?