如何获取命名空间中的所有控件?

den*_*eme 4 c# controls namespaces

如何获取命名空间中的所有控件?例如,我想获取System.Windows.Forms中的控件:TextBox,ComboBox等.

Dar*_*rov 6

命名空间中控件的概念有点不清楚.您可以使用反射来获取给定命名空间中从特定基类型派生的程序集中的类.例如:

class Program
{
    static void Main()
    {
        var controlType = typeof(Control);
        var controls = controlType
            .Assembly
            .GetTypes()
            .Where(t => controlType.IsAssignableFrom(t) && 
                        t.Namespace == "System.Windows.Forms"
            );
        foreach (var control in controls)
        {
            Console.WriteLine(control);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Far*_*ker 5

这将返回指定命名空间中的所有类:

string @namespace = "System.Windows.Forms";

var items = (from t in Assembly.Load("System.Windows.Forms").GetTypes()
        where t.IsClass && t.Namespace == @namespace
        && t.IsAssignableFrom(typeof(Control))
        select t).ToList();
Run Code Online (Sandbox Code Playgroud)