pavanarya

Let us share the knowledge

Looping On The Properties Of An Object

with 2 comments

Hi,
Happy New Year to my dear friends and developers across the globe.

In this post i want to discuss about the technique of looping on the properties of an object created using Reflection.
Reflection is process of accessing the assembly’s metadata that is information about properties, methods, parameters at run time. And this the class that i love most.

Let us take a class with some properties.

public class MessageContract
{
	
	public string property1 {get;set;}
        public string property2 {get;set;}
        public int property3 {get;set;}
        public int property4 {get;set;}

}

So in the above class MessageContract i am having few properties. So i want to loop on the properties of this class.

protected void Page_Load(object sender, EventArgs e)
    {
        MessageContract msg = new MessageContract();
        msg.property1 = "Pavan";
        msg.property2 = "Kumar";
        msg.property3 = 12;
        msg.property4 = 34;
        bool retValue = LoopOnProperties(msg);
    }

So i am creating a function that adds values to the properties of MessageContract class and calling a function LoopOnProperties that accepts object of MessageContract class.
So we are going to have actual logic of looping on the properties of MessageContract class in the function LoopOnProperties.
In order to use Reflection we should add System.Reflection namespace.

public bool LoopOnProperties(MessageContract msgcontract)
    {
        Type t = msgcontract.GetType();//Type.GetType("MessageContract");
        PropertyInfo[] propertyArray= t.GetProperties();
        foreach (PropertyInfo p in propertyArray)
        {
            object propertyValue = p.GetValue(msgcontract, null);
        }
        
        
        return true;
    }

So inorder to retrive the properties information of MessageContract i am using reflection.

Type class is the root class for System.Reflection. We are going to make use of Type class’s methods for invoking the members,properties and other information.

So in our method LoopOnProperties we are initially creating object for Type class which can be used to access the information of an assembly.

An object reference for a Type class can be returned in a number of ways and a few are

        Type t1 = msgcontract.GetType();
        Type t2 = Type.GetType("MessageContract");
        Type t3 = typeof(MessageContract);

There is a function associated with the type known as GetProperties() which returns an array of PropertyInfo objects. PropertyInfo objects in turn contain the actual data related to properties. So in order to loop on the properties of a class we are looping on the PropertyInfo[] array.

If our class MessageContract contains only public properties then we can make use of GetProperties() method of Type class.
If our class MessageContract contains non public properties then we are supposed to send a parameter “BindingFlags bindingAttr” to GetProperties(BindingFlags bindingAttr). BindingFlags contains an enumeration of values which specifies the type of data we want to access that is whether the data is either public or private and other stuff.

PropertyInfo[] propertyArray= t.GetProperties(BindingFlags.NonPublic);
//BindingFlags.NonPublic is used to specify that we are going to access private variables of that type.
PropertyInfo[] propertyArray= t.GetProperties();
        foreach (PropertyInfo p in propertyArray)
        {
            object propertyValue = p.GetValue(msgcontract, null);
             string propertyName=p.Name;
        }

so after getting the array of propertyInfo[] by using GetProperties() method our next step is to loop on that array.

GetValue() method of propertyInfo class return the value associated to property.

PropertyInfo.Name is used to return the name of the property.

Full Code:

1.Create a class file and name it as MessageContract and create a class by adding the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class MessageContract
{
	
		public string property1 {get;set;}
        public string property2 {get;set;}
        public int property3 {get;set;}
        public int property4 {get;set;}

}

2.Create an aspx page (Default.aspx)
3.In Default.aspx.cs file ad following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MessageContract msg = new MessageContract();
        msg.property1 = "Pavan";
        msg.property2 = "Kumar";
        msg.property3 = 12;
        msg.property4 = 34;
        bool retValue = LoopOnProperties(msg);
    }
    public bool LoopOnProperties(MessageContract msgcontract)
    {
        //Type t = msgcontract.GetType();//Type.GetType("MessageContract");
        Type t = typeof(MessageContract);
        PropertyInfo[] propertyArray= t.GetProperties(BindingFlags.NonPublic);
        foreach (PropertyInfo p in propertyArray)
        {
            object propertyValue = p.GetValue(msgcontract, null);
            string propertyName = p.Name;
        }

       // Type t1 = msgcontract.GetType();
       // Type t2 = Type.GetType("MessageContract");
       // Type t3 = typeof(MessageContract);

        return true;


    }
}

Have a great new year.

Thanks,
Pavan

About these ads

Written by pavanarya

December 31, 2011 at 3:42 pm

Posted in Asp.net, Reflection

2 Responses

Subscribe to comments with RSS.

  1. Nice Article, it is very useful

    We can submit .net related article links on http://www.dotnettechy.com/ to increase traffics

    Micle

    January 3, 2012 at 2:27 pm

  2. I just like the valuable information you supply to your articles. I will bookmark your blog and take a look at again right here regularly. I am slightly certain I will learn lots of new stuff proper here! Good luck for the next!

    Ivory Stewert

    January 20, 2012 at 12:22 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: