Wednesday 13 February 2008

Obtaining generic list item instances using reflection

I've decided to use my blog as a space to share what I think may be useful links, code examples, and information about ASP.NET, C# and general software engineering tips or workaround's I may have come upon.

So let me begin with a simple bit of code that may prove useful to some people.

I had a problem where I was using reflection that required a method to be invoked. This method returned a generic list of items. The problem was that using reflection the type that was returned from the method was "System.Collections.Generic.List`1[T]", and I needed a way to loop through the actual items in the list, in order to check types and property values.

The solution is pretty straightforward. There is a method in a generic list object that allows you to retrieve a single item based on it's index. The method name is "get_Item". Below is a small section of C# code that will show you how it works:

// Create an instance of the type you require
Type myType = "AssemblyName.TypeName, AssemblyName";
object instance = Activator.CreateInstance(typeof(myType));
// Invoke the method
MethodInfo method = myType.GetType().GetMethod("MethodName");
// The list object is of type "System.Collections.Generic.List`1[T]"
object list = method.Invoke(instance, null);
// Get the list item count
int listCount = (int)list.GetType().GetProperty("Count").GetValue(list, null);
// Loop though generic list
for (int index = 0; index <= listCount; index++)
{
// Get an instance of the item in the list
object item = list.GetType().GetMethod("getItem").Invoke(list, new object[] { index });
// Now you have an instance of the item in the generic list
//....
// TODO: Place additional code here/
//....
}

Hope that may help some of you out there. :)