C# - 将对象转置为值列表

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

但是,我想在前端将此对象显示为 table。 (前端在 Angular2 中)。我认为最好的方法是将对象转换为字典或类似的方法,但我不确定最佳方法或如何实施,因此我们将不胜感激。

或者如果有任何 angular2 专家有办法使用数据显示在 table 中,我洗耳恭听。我正在为我的组件使用 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;
}