2017-07-07 48 views
0

我有一些類都是從其他類繼承的。每個類只能從另一個類繼承,而不能從兩個類繼承。 我還得到了一個基類,它是我的繼承樹的「頂部」。 類是例如:根據繼承來排序屬性列表

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

不過,我需要的順序,這些屬性 「頂部至底部」,因此:

  1. _ID
  2. GlobalId
  3. OwnerHistory
  4. 名稱
  5. 描述
  6. 對象類型
  7. ObjectPlacement
  8. 表示
  9. PredefinedType

Therfore我想在每一個類中實現的方法,你可以調用,並且將在正確的順序性排序。該方法是這樣的,到目前爲止:

 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; 
      } 
     } 

有沒有人有一個想法如何訪問父oject並不僅僅是父類型,我在做我當前的代碼?還有其他建議如何解決這個問題?

+1

爲什麼它關係到屬性的順序?這聽起來像是一個XY問題。 – Sweeper

+0

我創建的對象在我的情況下,IfcAlignment-Object需要根據標準(ISO 10303-21)映射到字符串上。 ISO聲明這些屬性需要從上至下列出。結果字符串如下所示:#_ID = IFCALIGNMENT(GlobalId,OwnerHistory,Name,...)。 – FlixFix

+0

linq orderby:'foreach(var prop in genuineProps.OrderBy(g => g.Name))' –

回答

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(); 
+0

當我很快嘗試它時,它就起作用了!非常感謝!只是最後一個問題:我現在正在將其實施到另一種方法中。我可以不用說typeof(IfcAlignment)也可以用類型typeof(this.GetType())來解決這個問題,所以我不必爲每個實體重寫這段代碼? – FlixFix

+0

@FlixFix是的。你可以使用typeof(this.GetType()),或者如果你使用另一個類,只需要獲得對象typeof(obj.GetType()) – VenkyDhana