继承DataGridViewColumn的自定义列,在DataGridView的columns中能显示,他用的是什么机制?
public class BaseClass
{
    public BaseClass()
    {}
}
public class SubClass1:BaseClass
{}
public class SubClass2:BaseClass
{}
....
//如何在此处得到上面所有继承自BaseClass的所有类呢?
如何枚举?

解决方案 »

  1.   

    DataGridView 只知道 是 DataGridViewColumn 类型就够了他不关心你的具体类型中特异性的东西, 那些由你来负责.反射遍历类型可以通过 Assembly 的各个函数来做,
    遍历每个 Type 使用 Type 的各个函数来测试类型之间的关系.IsSubclassOf
    IsAssignableFrom
    之类的.详情参照 MSDN 例子代码.
      

  2.   

    class Program
    {
        static void Main(string[] args)
        {
            var subTypeQuery = from t in Assembly.GetExecutingAssembly().GetTypes()
                               where IsSubClassOf(t, typeof(Base))
                               select t;        foreach (var type in subTypeQuery)
            {
                Console.WriteLine(type);
            }
        }    static bool IsSubClassOf(Type type, Type baseType)
        {
            var b = type.BaseType;
            while (b!=null)
            {
                if (b.Equals(baseType))
                {
                    return true;
                }
                b = b.BaseType;
            }
            return false;
        }}public class Base { }
    public class Sub1 : Base { }
    public class Sub2 : Base { }
    public class Sub3 : Sub1 { } 
      

  3.   

    题目和问题不相干么
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;namespace WindowsApplication157
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            foreach (Type T in this.GetType().GetNestedTypes())
                    if (T.IsSubclassOf(typeof(BaseClass)))
                        MessageBox.Show(T.Name);
            }        public class BaseClass
            {
                public BaseClass()
                {
                }
            }        public class SubClass1 : BaseClass
            {
            }        public class SubClass2 : BaseClass
            {
            }
        }
    }