私はすべてのクラスが他のクラスから継承しています。各クラスは、ほかのクラスから継承するクラスであり、決して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の性質やループを呼び出すと、私はこの順序でそれらを得る:
- PredefinedType
- Axisを
- ObjectPlacement
- 表現
- のObjectType
- GlobalId
- OwnerHistory
- 名前 "上から下へ" の順序で
- 説明
- _ID
私はこれらのプロパティを必要とするしかしそう:
- _ID
- GlobalId
- OwnerHistory名前
- 説明 私はあなたが呼び出すことができますし、それが正しい順序でプロパティを並べ替えるだろうと、すべてのクラスにメソッドを実装したかった10
- のObjectType
- ObjectPlacement
- 表現
- 軸
- は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にアクセスし、私は私の現在のコードでやっている親の型だけでなく、アイデアを持っていますか?問題を解決するための他の提案はありますか?
プロパティはどのような順序で来るのですか?これはXY問題のように聞こえます。 – Sweeper
私が作成しているオブジェクトは、IfcAlignment-Objectをノルム(ISO 10303-21)に従った文字列にマップする必要があります。そして、ISOは、プロパティが上から下にリストされる必要があると述べている。結果の文字列は、#_ID = IFCALIGNMENT(GlobalId、OwnerHistory、Name、...)のようになります。 – FlixFix
linq orderbyについて:foreach(本物のプロップで注文した注文(g => g.Name)) ' –