コード例 #1
0
ファイル: AutoMapper.cs プロジェクト: redbaty/excel-mapper
        private static bool InferMapping <T>(this MemberInfo member, FallbackStrategy emptyValueStrategy, out ExcelPropertyMap mapping)
        {
            if (member.AutoMap(emptyValueStrategy, out SingleExcelPropertyMap <T> singleMapping))
            {
                mapping = singleMapping;
                return(true);
            }

            if (member.MemberType().GetElementTypeOrEnumerableType(out Type elementType))
            {
                MethodInfo method = AutoMapEnumerableMethod.MakeGenericMethod(elementType);

                var  parameters = new object[] { member, emptyValueStrategy, null };
                bool result     = (bool)method.Invoke(null, parameters);
                if (result)
                {
                    mapping = (ExcelPropertyMap)parameters[2];
                    return(true);
                }
            }

            if (member.AutoMapObject(emptyValueStrategy, out ObjectExcelPropertyMap <T> objectMapping))
            {
                mapping = objectMapping;
                return(true);
            }

            mapping = null;
            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Creates a map for a property or field given a MemberExpression reading the property or field.
        /// This is used for mapping objects that contain nested objects, primitives or enumerables.
        /// </summary>
        /// <typeparam name="TProperty">The element type of property or field to map.</typeparam>
        /// <param name="expression">A MemberExpression reading the property or field.</param>
        /// <returns>The map for the given property or field.</returns>
        public ObjectExcelPropertyMap <TProperty> MapObject <TProperty>(Expression <Func <T, TProperty> > expression)
        {
            MemberExpression memberExpression = GetMemberExpression(expression);
            MemberInfo       member           = memberExpression.Member;

            if (!member.AutoMapObject(EmptyValueStrategy, out ObjectExcelPropertyMap <TProperty> mapping))
            {
                throw new ExcelMappingException($"Could not map object of type \"{typeof(TProperty)}\".");
            }

            AddMapping(mapping, expression);
            return(mapping);
        }