[英]C# - Transpose an object into a list of values
我有一個簡單的對象,如下所示:
public partial class RepackViewIndex
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public DateTime? DateRequested { get; set; }
public string OrderNo { get; set; }
[StringLength(30)]
public string Customer { get; set; }
[StringLength(30)]
public string RepackFrom { get; set; }
[StringLength(30)]
public string RepackTo { get; set; }
public int? Qty { get; set; }
public int? ReworkTime_c { get; set; }
[StringLength(8)]
public string TTLTime { get; set; }
public int? QtyCompleted { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? PlannedCompletion { get; set; }
[StringLength(50)]
public string RequestedBy { get; set; }
[StringLength(30)]
public string EnteredBy { get; set; }
[StringLength(1)]
public string Status { get; set; }
[StringLength(50)]
public string Priority { get; set; }
}
但是,我想將此對象顯示為前端的表格。 (前端在Angular2中)。 我認為最好的方法是將對象轉換為Dictionary或類似的字典,但是我不確定最好的方法或實現方法,因此將不勝感激。
或者,如果外面有任何angular2專家都可以使用數據來顯示在表格中,那么我將不勝枚舉。 我正在為我的組件使用PrimeNG。
非常感謝,
如果您只是想按屬性名稱將值用作字典,則可以添加實例方法並使用反射:
public Dictionary<string, object> ToDictionary()
{
var dict = new Dictionary<string, object>();
foreach (var prop in GetType().GetProperties())
{
dict.Add(prop.Name, prop.GetValue(this));
}
return dict;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.