Wednesday, August 25, 2010

Applications of Extension methods

Extension methods was introduced some time back with C#3.0 .The feature is great we can attach one more function to the existing classes and can call those methods with the object of those classes itself.It is very helpful for architects to write all the required methods and attach with the existing classes as well.

We all might have surprised by seeing so many methods suddenly in our favorite IEnumerable derived classes such as List and observable collection when the linq is introduced.Did they wrote all these methods inside the List class? absolutely not.Instead they wrote extension methods for IEnumerable and from the user point of view they looks like normal methods because the methods can be invoked  just by using the objects.

I was looking for some uses of the extension methods like this.The option is to replace all most all the helper classes by extension methods. While looking through the helper classes I struck around the reflection helper. The code was written to get the value of property using reflection.If the developer is familiar with the code base he probably knows that there is a helper library and can use it.But what about a fresher.He will surely struck on this and may think about changing the design.If he is coming from the true object oriented world where there is no chance to invoke private properties he is done. The GetProperty and SetProperty really comes helpful here.

Don’t feel so much complicated .Its so simple .Write 2 extension methods which internally gets and sets the value of properties by getting the name of property in string.Now it comes very easy to the end developer as the method will be shown when they type the magic key dot(.)

public static class MyExtensions 
{
/// <summary>
/// Gets the value of property mentioned in propertyName.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static T GetProperty<T>(this object obj, string propertyName)
{
PropertyInfo pi = obj.GetType().GetProperties().FirstOrDefault((p) => string.Equals(p.Name,propertyName));
T t = default(T);
if (pi != null && typeof(T) == pi.PropertyType)
{
t = (T)pi.GetValue(obj, new object[] { });
}
return t;
}
/// <summary>
/// Sets the value of property mentioned in propertyName.Returns true on success.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SetProperty<T>(this object obj, string propertyName,T value)
{
PropertyInfo pi = obj.GetType().GetProperties().FirstOrDefault((p) => string.Equals(p.Name, propertyName));
if (pi != null && typeof(T) == pi.PropertyType)
{
pi.SetValue(obj,value, new object[] { });
return true;
}
return false;
}
}



Before using extension methods make sure that the assembly is referred and using the namespace.




SampleVM vm = new SampleVM();
vm.StringProperty = "Joy";
string s = vm.GetProperty<string>("StringProperty");
vm.SetProperty<string>("StringProperty", "Joymon");

1 comment:

Blogger said...

Get all your favorite alcoholic drinks on Duty Free Depot!

All the popular brand name beverages for unbeatable discounted price tags.