我在物件及其內部物件下方,我已將自定義屬性添加到報告所需的屬性名稱中。
public class Space
{
public SpaceIdentity SpaceIdentity { get; set; } = new();
public SpaceGeometry SpaceGeometry { get; set; } = new();
public AirBalance AirBalance { get; set; } = new();
public EngineeringChecks EngineeringChecks { get; set; } = new();
}
public class SpaceIdentity
{
public int ElementId { get; set; } // Not required
[DisplayNameWithUnits(DisplayName = "Space Number", IsIncludedInReport2 = true)]
public string Number { get; set; }
[DisplayNameWithUnits(DisplayName = "Space Name", IsIncludedInReport2 = true, IsIncludedInReport1 = true)]
public string Name { get; set; }
[DisplayNameWithUnits(DisplayName = "Room Number", IsIncludedInReport1 = true)]
public string RoomNumber { get; set; }
[DisplayNameWithUnits(DisplayName = "Room Name", IsIncludedInReport1 = true)]
public string RoomName { get; set; }
}
public class SpaceGeometry
{
public Vertex LocationPoint { get; set; } // this is not required
[DisplayNameWithUnits(DisplayName = "Space Area", Units = "(ft2)", IsIncludedInReport1 = true)]
public double FloorArea { get; set; }
}
.....
在這里,我正在構建一個 excel 報告,我想使用屬性顯示名稱作為該報告的標題列名稱。以下是多個報告中使用的一些屬性屬性資訊。我所做的是我添加了一個布爾條件屬性,如 ( isIncludedInReport1) 并回圈遍歷space內部物件 ( SpaceGeometry)的屬性并回圈遍歷內部物件 ( ) 的屬性,以根據此布爾條件獲取特定的屬性名稱及其屬性值。
我在這里尋找的是不添加這些 bool 屬性,是否有任何方法可以根據條件訪問屬性名稱。我想過添加介面,但在這里是不可能的,因為我有多個內部類具有需要包含在單個報告中的屬性。
任何人都可以讓我知道有沒有其他方法可以實作這一目標?
更新:
var columnResult = new OrderedDictionary();
GetReportHeaderColumnName(typeof(Space), columnResult);
public static void GetReportHeaderColumnName(Type type, OrderedDictionary headerNameByUnit)
{
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
if (propertyInfo.PropertyType.IsClass && !propertyInfo.PropertyType.FullName.StartsWith("System."))
{
if (propertyInfo.PropertyType == typeof(Overridable<double>))
{
AddReportHeaderName(headerNameByUnit, propertyInfo);
}
else
{
GetReportHeaderColumnName(propertyInfo.PropertyType, headerNameByUnit);
}
}
else
{
AddReportHeaderName(headerNameByUnit, propertyInfo);
}
}
}
protected static void AddReportHeaderName(OrderedDictionary columnResult, PropertyInfo propertyInfo)
{
if (propertyInfo.GetCustomAttributes(typeof(DisplayNameWithUnitsAttribute), true).Any())
{
var displayNameWithUnitsAttribute = (DisplayNameWithUnitsAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DisplayNameWithUnitsAttribute));
if (displayNameWithUnitsAttribute.IsIncludedInReport2)
{
columnResult.Add(displayNameWithUnitsAttribute.DisplayName, displayNameWithUnitsAttribute.Units);
}
}
}
uj5u.com熱心網友回復:
另一種不使用反射的方法是只使用一個串列 ReportProperty
public record ReportProperty<T>(
Func<T, string> ValueFunc,
string DisplayName,
string? Unit = null
);
List<ReportProperty<Space>> report1 = new(){
new( s => s.SpaceIdentity.Number, "Space Number"),
new( s => s.SpaceIdentity.RoomNumber, "Room Number"),
new( s => s.SpaceIdentity.RoomName, "Room Name"),
new( s => s.SpaceGeometry.FloorArea.ToString(), "Floor Area", "Ft2"),
};
uj5u.com熱心網友回復:
你想通過另一種方式實作什么?我假設您想讓您的代碼可擴展,以便添加更多報告更容易。
您還想在類本身上使用屬性嗎?如果是這樣,您可以創建一個新屬性,并像這樣標記您的屬性:
public class SpaceIdentity
{
public int ElementId { get; set; } // Not required
[DisplayNameWithUnits(DisplayName = "Space Number")]
[IncludeInReport(2)]
public string Number { get; set; }
[DisplayNameWithUnits(DisplayName = "Space Name")]
[IncludeInReport(1)]
[IncludeInReport(2)]
public string Name { get; set; }
[DisplayNameWithUnits(DisplayName = "Room Number")]
[IncludeInReport(1)]
public string RoomNumber { get; set; }
[DisplayNameWithUnits(DisplayName = "Room Name")]
[IncludeInReport(1)]
public string RoomName { get; set; }
}
或者您想將報告與類分開,以便在另一個檔案中定義您的報告?如果是這樣,也許屬性名稱的二維串列可以解決問題:
List<List<string>> Report1 = new()
{
new(){nameof(Space.SpaceIdentity), nameof(SpaceIdentity.Name)},
new(){nameof(Space.SpaceIdentity), nameof(SpaceIdentity.RoomNumber)},
new(){nameof(Space.SpaceIdentity), nameof(SpaceIdentity.RoomName)},
new(){nameof(Space.SpaceGeometry), nameof(SpaceGeometry.FloorArea)}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313590.html
