.NET:获取从特定类派生的所有类

标签 .net oop reflection inheritance attributes

我有一个自定义控件和一些从它派生的控件。
我需要获取当前程序集中从主类派生的所有类并检查它们的属性。
如何做到这一点?

最佳答案

var type = typeof(MainClass);

var listOfDerivedClasses = Assembly.GetExecutingAssembly()
    .GetTypes()
    .Where(x => x.IsSubclassOf(type))
    .ToList();

foreach (var derived in listOfDerivedClasses)
{
   var attributes = derived.GetCustomAttributes(typeof(TheAttribute), true);

   // etc.
}

关于.NET:获取从特定类派生的所有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5045297/

相关文章:

Java设计模式/转换模式

PHP:调用动态函数,但在类中?

java - 尝试从数据定义类获取默认值

c# - GetValue(...) - 错误在运行时被忽略

.net - System.Reflection - 全局方法不可用于反射

c# - 事件、委托(delegate)与 Action<T>

.net - 大字符串而在 POST 期间忽略空格后的字母

c# - 在不同的 AppDomain 中加载/卸载程序集

.net - 获取 asp.NET MVC 中的当前用户,以便我可以检查在用户控件中要执行的操作

c# - .Net Mvc : How to fire a error for Application_Error() manage them?