public static Type[] GetElementTypes(Type enumerableType, IEnumerable enumerable, ElemntTypeFlags flags = ElemntTypeFlags.None) { if (enumerableType.HasElementType) { return(new[] { enumerableType.GetElementType() }); } Type idictionaryType = enumerableType.GetDictionaryType(); if (idictionaryType != null && flags.HasFlag(ElemntTypeFlags.BreakKeyValuePair)) { return(idictionaryType.GetTypeInfo().GenericTypeArguments); } Type ienumerableType = enumerableType.GetIEnumerableType(); if (ienumerableType != null) { return(ienumerableType.GetTypeInfo().GenericTypeArguments); } if (typeof(IEnumerable).IsAssignableFrom(enumerableType)) { var first = enumerable?.Cast <object>().FirstOrDefault(); return(new[] { first?.GetType() ?? typeof(object) }); } throw new ArgumentException($"Unable to find the element type for type '{enumerableType}'.", nameof(enumerableType)); }
public static Type[] GetElementTypes(Type enumerableType, IEnumerable enumerable, ElemntTypeFlags flags = ElemntTypeFlags.None) { if (enumerableType.HasElementType) { return new[] {enumerableType.GetElementType()}; } Type idictionaryType = enumerableType.GetDictionaryType(); if (idictionaryType != null && flags.HasFlag(ElemntTypeFlags.BreakKeyValuePair)) { return idictionaryType.GetTypeInfo().GenericTypeArguments; } Type ienumerableType = enumerableType.GetIEnumerableType(); if (ienumerableType != null) { return ienumerableType.GetTypeInfo().GenericTypeArguments; } if (typeof(IEnumerable).IsAssignableFrom(enumerableType)) { var first = enumerable?.Cast<object>().FirstOrDefault(); return new[] {first?.GetType() ?? typeof(object)}; } throw new ArgumentException($"Unable to find the element type for type '{enumerableType}'.", nameof(enumerableType)); }
public static Type[] GetElementTypes(Type enumerableType, ElemntTypeFlags flags = ElemntTypeFlags.None) { return GetElementTypes(enumerableType, null, flags); }
public static Type[] GetElementTypes(Type enumerableType, ElemntTypeFlags flags = ElemntTypeFlags.None) { return(GetElementTypes(enumerableType, null, flags)); }