C# Printing Properties
- by Mark
I have a class like this with a bunch of properties:
class ClassName
{
string Name {get; set;}
int Age {get; set;}
DateTime BirthDate {get; set;}
}
I would like to print the name of the property and it's value using the value's ToString() method and the Property's name like this:
ClassName cn = new ClassName() {Name = "Mark", Age = 428, BirthData = DateTime.Now}
cn.MethodToPrint();
// Output
// Name = Mark, Age = 428, BirthDate = 12/30/2010 09:20:23 PM
Reflection is perfectly okay, in fact I think it is probably required. I'd also be neat if it could somehow work on any class through some sort of inheritance. I'm using 4.0 if that matters.