Go to DotNetFunda.com
 Online : 871 |  Welcome, Guest!   Login
 
Home > Articles > .NET Framework > How to iterate thought all properties of a class

  • Nominate yourself for "Agile Software Development using Scrum" online session for FREE, click here.

  • Download OOPS and ASP.NET Online training session video and PPT from here.

Submit Article | Articles Home | Search Articles |

How to iterate thought all properties of a class

 Download source file
 Posted on: 9/7/2008 1:34:25 AM by Vezzali | Views: 2345 | Category: .NET Framework | Level: Intermediate | Print Article
The purpose of this article is to describe some of the practical uses of the Reflection.

Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Introduction

The purpose of this article is to describe some of the practical uses of the
Reflection.
 

Reflection

Reflection is an important resource of the Microsoft Framework, for example,
enables us to get some information about object in runtime, in this article
we will iterate thought all properties of a class named Person, using Reflection.

Person is a simple class, used just to hold information about a Person.

The following C# .Net code shows how to do it.

Example Code

class Program
{
    static void Main(string[] args)
    {
        Person oPerson = new Person();
        oPerson.iAge = 27;
        oPerson.sName = "Fernando Vezzali";
        PropertyInfo[] properties = oPerson.GetType().GetProperties();
        foreach (PropertyInfo oPropertyInfo in properties)
        {
            MethodInfo oMethodInfo = oPerson.GetType().GetMethod("get_" + oPropertyInfo.Name);
            ParameterInfo[] ArrParameterInfo = oPerson.GetType().GetMethod("get_" + oPropertyInfo.Name).GetParameters();
            Console.WriteLine(oPropertyInfo.Name + " = " + oMethodInfo.Invoke(oPerson, ArrParameterInfo));
        }
        Console.Read();
    }
}

class Person
{
    private int _iAge;
    private string _sName;

    public int iAge
    {
        get { return _iAge; }
        set { _iAge = value; }
    }
    public string sName
    {
        get { return _sName; }
        set { _sName = value; }
    }
}
 

Conclusion

The Person class extends object and does not implement any interface. 
Using reflection we can iterate thought all properties.



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:8 year(s)
Home page:http://weblogs.asp.net/fernandovezzali
Member since:Sunday, September 07, 2008
Level:Starter
Status: [Member]
Biography:
 Latest post(s) from Vezzali

   ◘ How to iterate thought all properties of a class posted on 9/7/2008 1:34:25 AM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)