How to iterate through all properties of a class
Reflection is an important capability of the .NET framework and enables you to get information about objects at runtime. In this article, we will iterate through all properties for a class named Person using reflection.
using System;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Age = 27;
person.Name = "Fernando Vezzali";
Type type = typeof(Person);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine("{0} = {1}", property.Name, property.GetValue(person, null));
}
Console.Read();
}
}
The Person class extends System.Object and does not implement any interface. It is a simple class used to store information about a person:
class Person
{
private int age;
private string name;
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
Revision number 11, Saturday, August 08, 2009 12:04:30 PM by XIII
You must Login to comment.
|
Tue, Sep 9, 2008 5:47 AM
by cliquerz
|
This is cool. Two thumbs up!
|
|
Sun, Oct 26, 2008 9:49 PM
by joechung
|
What does this have to do with ASP.NET?
|
|
Mon, Oct 27, 2008 6:18 PM
by mbanavige
|
@joechung: While the example in this article currently uses a console app for demonstration purposes, reflection is still an applicable (and powerful) technique that can be used in ASP.NET web apps.
|
|
Tue, Dec 16, 2008 2:04 PM
by SyntaxC4
|
If you override the ToString() method of your dataobjects you can make a nice clean output of all of the properties in the object. It's great for tracing. I've been too busy or i would have gotten my example up. I don't know if i can add mine now because it's nearly a duplicate of this one.
|
|
Sun, Jan 4, 2009 6:46 AM
by rami_nassar
|
hope if you formate your article and provide vb code
|
|
Thu, Jan 8, 2009 11:34 AM
by SoulStoneBR
|
Good article! However, you forgot to add this using: using System.Reflection; And, of course, to define the namespace. ^^
|
|
Fri, Jan 9, 2009 9:26 PM
by SGWellens
|
Please fix the code formatting.
|
|
Mon, Feb 9, 2009 10:25 AM
by xmen
|
nice :^:
|
|
Sun, Feb 15, 2009 8:50 PM
by xmen
|
MethodInfo oMethodInfo = oPerson.GetType().GetMethod("get_" + oPropertyInfo.Name); ParameterInfo[] ArrParameterInfo = oPerson.GetType().GetMethod("get_" + oPropertyInfo.Name).GetParameters(); can be MethodInfo oMethodInfo = oPerson.GetType().GetMethod("get_" + oPropertyInfo.Name); ParameterInfo[] ArrParameterInfo = oMethodInfo.GetParameters();
|
|
Mon, Jul 13, 2009 11:04 AM
by kgb1774
|
Two thumbs up
|
|
Thu, Jul 23, 2009 10:38 AM
by Teos
|
Would it be, for arguement sake, possible to turn that into a public function, with a parameter for type?
e.g getPropInfo(Type oT), or somthing similar?
|
|
Wed, Jul 29, 2009 5:50 AM
by joechung
|
Absolutely. typeof(Person) is an instance of the Type class.
|
|
Wed, Aug 5, 2009 11:42 PM
by hajan
|
very simple and nice explained...
|
|
Tue, Aug 11, 2009 1:27 AM
by anoopsanduja
|
yeah.. nicely explained..but could have been more elaborated..by the way good one...!!!
|
|
Fri, Aug 14, 2009 7:56 AM
by csharppointer
|
good to start with reflection
|
|
Sat, Aug 15, 2009 2:32 AM
by vn_nilesh@hotmail.com
|
Along with iterating over the list of properties it might be hepful to explain other features of reflection like methodInfo, constructorInfo and about getiing parameter information of the methods at runtime. You can also add a bit of code to explain how to call methods using refelction.
|
|
Thu, Aug 27, 2009 11:56 AM
by Spi
|
Why the LinQ part has gone (see rev 7). It was not good ?
|
|
Fri, Oct 30, 2009 1:28 AM
by yrb.yogi
|
very simple and nice explained...
|
|
Wed, Feb 10, 2010 2:07 AM
by cnranasinghe
|
simple and nice
|
Revision #3
Tue, Sep 20, 2011 9:12 PM
by
|
Reflection
Reflection is the ability of the .NET framework to gather information (metadata) about assemblies, modules and types at runtime. It allows you also to dynamically create instances of types, invoke methods and access fields, properties and attributes. System
|
Revision #2
Mon, Jul 9, 2012 6:53 PM
by
|
Reflection in .NET
Reflection is one of the very nice features of .net, with Reflection one can take out lotsa information about the type. When I say type that means it could be a .net type that can be a class, enum, structure etc. with reflection you can get the methods of
|
New
Wed, Sep 23, 2009 7:26 PM
by
|
Get Information of Programatically Created Asp.Net page Using BuildManager class
Few days back I came across a problem on asp.net site wherein a user wanted to get information about controls of a Page from a different page. Problem with this approach is that server loads the requested page in memory. So at one time you can be only in one
|