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

Submit Article | Articles Home | Search Articles |

How to iterate thought all properties of a class

red flag  Posted on: 9/7/2008 1:34:25 AM by Vezzali | Views: 3169 | Category: .NET Framework | Level: Intermediate


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

Download


 Download source code for How to iterate thought all properties of a class



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.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Vezzali

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

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 4:02:00 AM