看基類被那幾個類了

gatran發表於2024-04-25
using System;
using System.Reflection;
using System.Linq;
 
public class BaseClass
{
}
 
public class DerivedClass1 : BaseClass
{
}
 
public class DerivedClass2 : BaseClass
{
}
 
class Program
{
    static void Main()
    {
        Type baseType = typeof(BaseClass);
        Assembly assembly = Assembly.GetExecutingAssembly();
 
        var derivedTypes = assembly.GetTypes()
            .Where(t => t.IsSubclassOf(baseType));
 
        foreach (var type in derivedTypes)
        {
            Console.WriteLine(type.Name);
        }
    }
}

  

相關文章