public static Type GetListItemType(object list) {
if (list == null) {
return null;
}
Type itemType = null;
// special case for IListSource
if ((list is Type) && (typeof(IListSource).IsAssignableFrom(list as Type))) {
list = CreateInstanceOfType(list as Type);
}
list = GetList(list);
Type listType = (list is Type) ? (list as Type) : list.GetType();
object listInstance = (list is Type) ? null : list;
if (typeof(Array).IsAssignableFrom(listType)) {
itemType = listType.GetElementType();
}
else {
PropertyInfo indexer = GetTypedIndexer(listType);
if (indexer != null) {
itemType = indexer.PropertyType;
}
else if (listInstance is IEnumerable) {
itemType = GetListItemTypeByEnumerable(listInstance as IEnumerable);
}
else {
itemType = listType;
}
}
return itemType;
}