.NET gives us a cool feature called Reflection using that we can access object’s properties and methods at run time. The classes which help us in using this feature are all in System.Reflection namespace. That means you will have to add this namespace in the class where you will be writing reflection code. So basically, when object type is unknown at design time, we can’t access its properties until we don’t know what properties object possesses. In such situation we can access object properties at runtime using Reflection.
Let’s head towards building a small example,
I have Red and Blue forms in my project and for both the forms I want to show a label on the form when form is in focus and make that label invisible when form is not in focus.
I have a common library where I have ShowBusy() method. Red and Blue form calls this method to get label visible and invisible on Enter and Leave events respectively.
Both forms also have two more properties:
1. GetProgressLabel: It is a read only property which will return progress label.
2. IsInProgress: This property will possess form’s status whether form is in progress or not. Once ShowBusy is done for a form, it can update this property for a form.
So Blue and Red form’s code looks like below:
Blue Form:
public partial class frmBlue : Form
{
private bool _isInProgress;
clsProgress objprog = new clsProgress();
public frmBlue()
{
InitializeComponent();
}
private void frmBlue_Enter(object sender, EventArgs e)
{
objprog.ShowBusy(this, true);
}
public Label GetProgressLabel
{
get { return lblProgress; }
}
public bool IsInProgress
{
get { return _isInProgress; }
set { _isInProgress = value; }
}
private void frmBlue_Leave(object sender, EventArgs e)
{
objprog.ShowBusy(this, false);
}
}
Red Form:
public partial class frmRed : Form
{
private bool _isInProgress;
clsProgress objprog = new clsProgress();
public frmRed()
{
InitializeComponent();
}
private void frmRed_Enter(object sender, EventArgs e)
{
objprog.ShowBusy(this, true);
}
public Label GetProgressLabel
{
get { return lblProgress; }
}
public bool IsInProgress
{
get { return _isInProgress; }
set { _isInProgress = value; }
}
private void frmRed_Leave(object sender, EventArgs e)
{
objprog.ShowBusy(this, false );
}
}
In the common Library, I have a class clsProgress in which I have defined ShowBusy as below:
public class clsProgress
{
public void ShowBusy(object sender, bool busy)
{
//Make Progress Label Visible/Invisible
#1 Type SenderType = sender.GetType();
#2 PropertyInfo pi = (PropertyInfo)SenderType.GetProperty("GetProgressLabel");
#3 MethodInfo mi = pi.GetGetMethod();
#4 System.Windows.Forms.Label lbl = (System.Windows.Forms.Label)mi.Invoke(sender, null);
#5 lbl.Visible = busy;
//Set IsInProgress property of the form
#6 PropertyInfo pi1 = (PropertyInfo)SenderType.GetProperty("IsInProgress");
#7 pi1.SetValue(sender, busy, null);
}
}
In the ShowBusy method, we have code to make form’s progress label visible/invisible. It also has logic to set IsInProgress property of the form.
In this case, it would have been difficult to access sender’s property directly because at design time it is not known for which form ShowBusy is called. Another point to be noted is, Common Library project does not have reference of Red and Blue form project, so if you are thinking of having alternate way of achieving this by adding if conditions then casting to red and blue form then it won’t be possible. To cast it to Red and Blue form, you will need to have Blue/Red form project reference added in Common Library. So we adopted the Reflection here to access and modify object’s properties.
Let’s understand what we have done in ShowBusy:
#1 : We are extracting the type out of sender object
#2 : Using the extracted type we are extracting GetProgressLabel property and storing the property information in PropertyInfo object.
#3 : It returns the “Get” accessor of the GetProcessLabel property.
#4 : Invoke method on MethodInfo object will invoke the get property and return its value.
#5 : At line #4 we got the form’s label, so accessing Visible property and setting it.
#6 : Like step #2, extracting IsInProgress property.
#7 : To set property value, we have SetValue method. Please have a look onto http://msdn.microsoft.com/en-us/library/xb5dd1f1.aspx to understand SetValue method’s parameters.
You can download the example from here.
Comments