If I have for example one class like
public class User{ public int Id { get; set; } public int Reputation { get; set; } public string DisplayName { get; set; } public DateTime LastAccessDate { get; set; } public DateTime CreationDate { get; set; } public string WebSiteUrl { get; set; } public int Views { get; set; } public int Age { get; set; } public int UpVotes { get; set; } public int downVotes { get; set; } public string Location { get; set; } public string AboutMe { get; set; } }
And I want to iterate through these fields dynamicly, for example to some method which will inspect passed object and it will return to caller its fields.
Is this possible ?
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With