Easiest way to get a common base class from a collection of types

后端 未结 6 1556
情书的邮戳
情书的邮戳 2020-12-17 05:07

I\'m building a custom property grid that displays the properties of items in a collection. What I want to do is show only the properties in the grid that are common amongst

6条回答
  •  悲&欢浪女
    2020-12-17 05:51

    Well,

    You could create in interface similar to IComparable but instead call it something like IPropertyComparable and then have the classes that implement it use reflection to compare their property names as so...

    public int Compare(T x, T y)
    {
         PropertyInfo[] props = x.GetType().GetProperties();
    
         foreach(PropertyInfo info in props)
         {
              if(info.name == y.GetType().Name)
              ....
         }
    
         ...
    

    I'll let you figure out the rest. It could probably be a little more elegant anyway, use LINQ maybe...

    • Matt

提交回复
热议问题