Home / ASP.NET Wiki / .NET Framework Essentials / Reflection / How to iterate through all properties of a class

How to iterate through all properties of a class

 Rate It (24)

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

Comments

This is cool. Two thumbs up!

What does this have to do with ASP.NET?

@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.

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.

hope if you formate your article and provide vb code

Good article! However, you forgot to add this using: using System.Reflection; And, of course, to define the namespace. ^^

Please fix the code formatting.

nice :^:

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();

Two thumbs up

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?

Absolutely. typeof(Person) is an instance of the Type class.

very simple and nice explained...

yeah.. nicely explained..but could have been more elaborated..by the way good one...!!!

good to start with reflection

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.

Why the LinQ part has gone (see rev 7). It was not good ?

very simple and nice explained...

simple and nice

Related Articles

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

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

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

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. francissvk (1)
  2. deepeshsp (1)