pavanarya – Pavan Kumar Aryasomayajulu <br> My New Blog http://xyzcoder.com

Contact Email : pavan.aryasomayajulu@gmail.com

Archive for December 2011

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

Written by pavanarya

December 31, 2011 at 3:42 pm

Posted in Asp.net, Reflection

%d bloggers like this: