Iterate through class fields and print them

后端 未结 1 764
暖寄归人
暖寄归人 2020-12-30 00:17

If I have for example one class like

public class User{
            public int Id { get; set; }
            public int Reputation { get; set; }
            p         


        
相关标签:
1条回答
  • 2020-12-30 00:55

    They're not fields, they're properties. You can use reflection to list them:

    User user = ...
    foreach(PropertyInfo prop in typeof(User).GetProperties())
    {
        Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(user, null));
    }
    
    0 讨论(0)
提交回复
热议问题