C# Printing Properties
Posted
by
Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-12-31T05:29:16Z
Indexed on
2010/12/31
5:54 UTC
Read the original article
Hit count: 392
c#
|reflection
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.
© Stack Overflow or respective owner