Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
 public static Type[] GetElementTypes(Type enumerableType, ElemntTypeFlags flags = ElemntTypeFlags.None)
 {
     return GetElementTypes(enumerableType, null, flags);
 }
Exemplo n.º 4
0
 public static Type[] GetElementTypes(Type enumerableType, ElemntTypeFlags flags = ElemntTypeFlags.None)
 {
     return(GetElementTypes(enumerableType, null, flags));
 }