本文介绍了让所有类型从基类派生的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查程序集的内容,发现它的所有类是直接或间接地从Windows.Forms.UserControl的。

I am trying to examine the contents of an assembly and find all classes in it that are directly or indirectly derived from Windows.Forms.UserControl.

我做的这样的:

Assembly dll = Assembly.LoadFrom(filename);
var types = dll.GetTypes().Where(x => x.BaseType == typeof(UserControl));



但它是给一个空列表,因为没有类直接扩展用户控件。我不知道有足够的了解反映迅速做到这一点,如果我没有我宁愿不写一个递归函数。

But it is giving an empty list because none of the classes directly extend UserControl. I don't know enough about reflection to do it quickly, and I'd rather not write a recursive function if I don't have to.

推荐答案

您应该使用这个代替:

You should use Type.IsSubclassOf this instead:

var types = dll.GetTypes().Where(x => x.IsSubclassOf(typeof(UserControl)));

这篇关于让所有类型从基类派生的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 12:11