private static object GetArrayElement(object instance, int index)
{
IList list = instance as IList;
if (list == null && instance != null && instance.GetType().IsGenericType)
{
Type instanceType = instance.GetType();
Type[] genArguments = instanceType.GetGenericArguments();
Type genType = instanceType.GetGenericTypeDefinition().MakeGenericType(genArguments);
// I'm not going to retest for IList implementation as
// if we got here, the AssertIsValidArray has run successfully
PropertyInfo countPropInfo = genType.GetProperty("Count");
int count = (int) countPropInfo.GetValue(instance, null);
if (count == 0 || index + 1 > count)
{
return null;
}
PropertyInfo indexerPropInfo = genType.GetProperty("Item");
return indexerPropInfo.GetValue(instance, new object[] { index });
}
if (list == null || list.Count == 0 || index + 1 > list.Count)
{
return null;
}
return list[index];
}