我有一些類都是從其他類繼承的。每個類只能從另一個類繼承,而不能從兩個類繼承。 我還得到了一個基類,它是我的繼承樹的「頂部」。 類是例如:根據繼承來排序屬性列表
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的性能和循環,我讓他們在這個順序:
- PredefinedType
- 軸
- ObjectPlacement
- 表示
- 對象類型
- GlobalId
- OwnerHistory
- 名稱
- 說明
- _ID
不過,我需要的順序,這些屬性 「頂部至底部」,因此:
- _ID
- GlobalId
- OwnerHistory
- 名稱
- 描述
- 對象類型
- 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(var prop in genuineProps.OrderBy(g => g.Name))' –