GetProperties() to return all properties for an interface inheritance hierarchy

前端 未结 6 771
北海茫月
北海茫月 2020-11-27 02:24

Assuming the following hypothetical inheritance hierarchy:

public interface IA
{
  int ID { get; set; }
}

public interface IB : IA
{
  string Name { get; se         


        
6条回答
  •  余生分开走
    2020-11-27 03:03

    Exactly the same problem has a workaround described here.

    FlattenHierarchy doesnt work btw. (only on static vars. says so in intellisense)

    Workaround. Beware of duplicates.

    PropertyInfo[] pis = typeof(IB).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    Type[] tt = typeof(IB).GetInterfaces();
    PropertyInfo[] pis2 = tt[0].GetProperties(BindingFlags.Public | BindingFlags.Instance);
    

提交回复
热议问题