根据继承排序属性列表

时间:2017-07-07 05:48:03

标签: c# inheritance properties early-binding

我有某些类都继承自其他类。每个类只继承自另一个类,从不继承两个类。 我还有一个Base-Class,它是我的继承树的“顶层”。 例如:类

public class IfcAlignment : IfcLinearPositioningElement
{
    public IfcAlignmentTypeEnum PredefinedType {get; set;}
}
public class IfcLinearPositioningElement : IfcProduct
{
   public IfcCurve Axis {get; set;}
}
public class IfcProduct : IfcObject
{
   public IfcObjectPlacement ObjectPlacement {get; set;}
   public IfcProductRepresentation Representation {get; set;}
}
public class IfcObject: IfcRoot
{
   IfcLabel ObjectType {get; set;}
}
public class IfcRoot : IfcBase
{
   public IfcGloballyUniqueId GlobalId {get; set;}
   public IfcOwnerHistory OwnerHistory {get; set;}
   public IfcLabel Name {get; set;}
   public IfcText Description {get; set;}
}
public abstract class IfcBase
{
   public int _ID {get; set;}
}

这是我的结构中的一组继承。当我现在调用IfcAlignment的属性并遍历它们时,我按顺序得到它们:

  1. PredefinedType
  2. ObjectPlacement
  3. 表示
  4. 对象类型
  5. GlobalId
  6. OwnerHistory
  7. 名称
  8. 描述
  9. _ID
  10. 但是我需要按照“从上到下”的顺序使用这些属性,所以:

    1. _ID
    2. GlobalId
    3. OwnerHistory
    4. 名称
    5. 描述
    6. 对象类型
    7. ObjectPlacement
    8. 表示
    9. PredefinedType
    10. 因此,我想在每个类中实现一个方法,您可以调用它,并按正确的顺序对属性进行排序。到目前为止,该方法看起来像这样:

              override public List<PropertyInfo> SortMyProperties(object entity)
              {
                  List<PropertyInfo> returnValue = new List<PropertyInfo>();
                  if (entity is IfcBase && !entity.GetType().Name.Contains("IfcBase"))
                  {
                      //Here I need to get the actual parent object:
                      // I tried the following, which did no work unfortunately:
                      // var parent = entity.GetType().BaseType;
      
                      PropertyInfo propInfo = parent.SortMyProperties(parent);
      
                      //get my own properties:
                      Type type = entity.GetType();
                      var genuineProps = typeof(/*type of the current class*/).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                      foreach (var prop in genuineProps)
                      {
                          returnValue.Add(prop);
                      }
                      return returnValue;
                  }
                  else
                  {
                      var properties = this.GetType().GetProperties();
                      foreach (var prop in properties)
                      {
                          returnValue.Add(prop);
                      }
                      return returnValue;
                  }
              }
      

      有没有人知道如何访问父对象而不仅仅是父类型,我正在使用当前代码?还有其他建议如何解决问题?

1 个答案:

答案 0 :(得分:0)

你能试试吗?这将是实现您的要求的更好解决方案

            var type = typeof(IfcAlignment);
            List<string> PropertyNames= new List<string>();
            while(type!=null)
            {
                  var properties = type.GetProperties().Where(x => x.DeclaringType == type).Select(x=>x.Name).Reverse().ToList();
                  foreach(string name in properties)
                  {
                       PropertyNames.Add(name);
                  }
                type = type.BaseType;

            }
            PropertyNames.Reverse();
相关问题