Exemplo n.º 1
0
        public static bool AutoMapEnumerable <T>(this MemberInfo member, FallbackStrategy emptyValueStrategy, out EnumerableExcelPropertyMap <T> map)
        {
            Type     rawType     = member.MemberType();
            TypeInfo rawTypeInfo = rawType.GetTypeInfo();

            if (!member.AutoMap(emptyValueStrategy, out SingleExcelPropertyMap <T> elementMapping))
            {
                map = null;
                return(false);
            }

            if (rawType.IsArray)
            {
                map = new ArrayPropertyMap <T>(member, elementMapping);
                return(true);
            }
            else if (rawTypeInfo.IsInterface)
            {
                if (rawTypeInfo.IsAssignableFrom(typeof(List <T>).GetTypeInfo()))
                {
                    map = new InterfaceAssignableFromListPropertyMap <T>(member, elementMapping);
                    return(true);
                }
            }
            else if (rawType.ImplementsInterface(typeof(ICollection <T>)))
            {
                map = new ConcreteICollectionPropertyMap <T>(rawType, member, elementMapping);
                return(true);
            }

            map = null;
            return(false);
        }
Exemplo n.º 2
0
        public static bool TryMapGenericEnumerable <T>(MemberInfo member, FallbackStrategy emptyValueStrategy, out EnumerableExcelPropertyMap <T> map)
        {
            Type     rawType     = member.MemberType();
            TypeInfo rawTypeInfo = rawType.GetTypeInfo();

            // First, get the mapper for the element. This is used to convert individual values
            // to be added to/included in the collection.
            if (!TryMapPrimitive(member, emptyValueStrategy, out SingleExcelPropertyMap <T> elementMapping))
            {
                map = null;
                return(false);
            }

            // Secondly, find the right way of adding the converted value to the collection.
            if (rawType.IsArray)
            {
                // Add values using the arrray indexer.
                map = new ArrayPropertyMap <T>(member, elementMapping);
                return(true);
            }
            else if (rawTypeInfo.IsInterface)
            {
                // Add values by creating a list and assigning to the property.
                if (rawTypeInfo.IsAssignableFrom(typeof(List <T>).GetTypeInfo()))
                {
                    map = new InterfaceAssignableFromListPropertyMap <T>(member, elementMapping);
                    return(true);
                }
            }
            else if (rawType.ImplementsInterface(typeof(ICollection <T>)))
            {
                // Add values using the ICollection<T>.Add method.
                map = new ConcreteICollectionPropertyMap <T>(rawType, member, elementMapping);
                return(true);
            }

            map = null;
            return(false);
        }