我有一个自定义控件和一些从它派生的控件。
我需要获取当前程序集中从主类派生的所有类并检查它们的属性。
如何做到这一点?
最佳答案
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/