public static bool TryGetListType(this Type type, out Type nestedType) { Type genericTypeDef = null; Type listType = null; try { genericTypeDef = type.GetGenericTypeDefinition(); } catch (Exception) {} if (type.IsArray) { listType = type; } else if (genericTypeDef != null && GenericListTypes.Any(t => t.IsAssignableFrom(genericTypeDef)) && type.GetGenericArguments().Count() == 1) { listType = type.GetGenericArguments().Single(); } else { listType = null; } nestedType = listType; return(listType != null); }
/// <summary> /// Check if the type is a generic list. /// </summary> public static bool IsGenericList(Type type) { return(GenericListTypes.Contains(type)); }