How to list all Variables of Class

前端 未结 5 1767
情书的邮戳
情书的邮戳 2020-11-27 05:49

Is there a way to list all Variables (Fields) of a class in C#. If yes than could someone give me some examples how to save them in a List and get them maybe us

5条回答
  •  清酒与你
    2020-11-27 06:16

    Not quite the question asked - but here's how to get the values of all properties of type Decimal from an object called "inv":

    var properties = inv.GetType().GetProperties();
    
    foreach (var prop in properties)
    {
        if (prop.ToString().ToLower().Contains("decimal"))
        {
            totalDecimal += (Decimal)prop.GetValue(inv, null);
        }
    }
    

提交回复
热议问题