2017-07-07 3 views
0

私はすべてのクラスが他のクラスから継承しています。各クラスは、ほかのクラスから継承するクラスであり、決して2つのクラスから継承するクラスはありません。 私はまた、私の継承ツリーの「トップ」であるベースクラスを持っています。 クラスは、例えば、次のとおりです。継承に従ったプロパティリストのソート

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

これは私の構造内の継承の1セットです。私は今、それらを介してIfcAlignmentの性質やループを呼び出すと、私はこの順序でそれらを得る:

  1. PredefinedType
  2. Axisを
  3. ObjectPlacement
  4. 表現
  5. のObjectType
  6. GlobalId
  7. OwnerHistory
  8. 名前
  9. "上から下へ" の順序で
  10. 説明
  11. _ID

私はこれらのプロパティを必要とするしかしそう:

  1. _ID
  2. GlobalId
  3. OwnerHistory名前
  4. 説明
  5. 私はあなたが呼び出すことができますし、それが正しい順序でプロパティを並べ替えるだろうと、すべてのクラスにメソッドを実装したかった10
  6. のObjectType
  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(本物のプロップで注文した注文(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

関連する問題