I ran into an issue today using reflection. I was getting properties on a object using the following code:

1: Type argsType = args.GetType();

2: PropertyInfo piResults = argsType.GetProperty("Result");

3:object result = piResults.GetValue(args, null);

This works great if the object you are trying to retrieve is just a simple object. However, what if the object in question is a collection of some sort? This does not work because the results of type object are useless. The key to solving this issue is to use the IEnumerable interface. Here is the code to solve my issue:

Notice on line 4 I’m casting the results from the GetValue method to an IEnumerable. From there I can run through the objects and use reflection to pull out the individual properties. Not the cleanest approach, but it works well if you are in a situation where the type of your results are not known to the consumer.