我需要根据一个接口所定义的属性Property,来生成一个DataTable,也就是需要能遍历一个接口的所有方法和属性。接口是在一个DLL中的,用反射来读取DLL我知道,但是如何做到遍历属性?

解决方案 »

  1.   

    Type type = entity.GetType();
                int count = 0;
                foreach (Attribute att in type.GetCustomAttributes(false))
                {
                    if (att is DBTable)
                    {
                        sb.Append(GetHtmlFrom(att as DBTable, colcount));
                    }       
                }
                foreach (PropertyInfo pi in type.GetProperties())
                {
      

  2.   

    1.通过你的实例找到它的类型(Type)对象2.调用Type的的FindInterfaces 方法获得你的Property的接口类型
    public virtual Type[] FindInterfaces (
    TypeFilter filter,
    Object filterCriteria
    )这里注意一下TypeFilter和filterCriteria,看看MSDN中例子应该知道怎么做
    不把例子贴出来了.3.然后就通过你获得接口类型和你的原来的实例通过反射:GetPropreties(),GetMethods方法就可以获得所有的方法和属性