Here I am trying to add a quick list of elementary functions that are quite often used. Few more I have on my cards are: 1. Random string generator based on a custom alphabet set. 2. Object compare using serialization etc etc You are free to come up with your suggestions, I will try to add them here when time permits. Meanwhile few things that shouldn't go in here are the standard algorithms, like Sorting algorithms, Shortest path etc as they are very freely available on the internet.

Sunday, May 2, 2010

Get and set property values using reflection

/// <summary/>

/// This method returns the value of a property from an object instance

/// using reflection.

/// </summary>

/// <param name="argTargetType">

/// The object instance that contains the property whose value is to be retreived.

/// </param>

/// <param name="argPropertyName">

/// The name of the property whose value is to be retreived.

/// </param>

/// <returns></returns>

public static object GetPropertyValue(object argParentObject,

string argPropertyName)

{

//Get the System.Type of the parent object.

Type objectType = argParentObject.GetType();

if (argParentObject == null

|| objectType == null)

return null;

//Instantiate a PropertyInfo object that corresponds to the required property

PropertyInfo propertyInfo = objectType.GetProperty(argPropertyName);

if (propertyInfo == null)

return null;

//Retrieve the property value as and object

object obj = propertyInfo.GetValue(argParentObject, null);

return obj;

}

Followers

About Me

My photo
Simple straight forward