Example #1
0
        /// <summary>
        /// Auto maps all members for the given type. If a member
        /// is mapped again it will override the existing map.
        /// </summary>
        /// <param name="context">The context.</param>
        public virtual void AutoMap(CsvContext context)
        {
            var type = GetGenericType();

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                throw new ConfigurationException("Types that inherit IEnumerable cannot be auto mapped. " +
                                                 "Did you accidentally call GetRecord or WriteRecord which " +
                                                 "acts on a single record instead of calling GetRecords or " +
                                                 "WriteRecords which acts on a list of records?");
            }

            var mapParents = new LinkedList <Type>();
            var args       = new ShouldUseConstructorParametersArgs(type);

            if (context.Configuration.ShouldUseConstructorParameters(args))
            {
                // This type doesn't have a parameterless constructor so we can't create an
                // instance and set it's member. Constructor parameters need to be created
                // instead. Writing only uses getters, so members will also be mapped
                // for writing purposes.
                AutoMapConstructorParameters(this, context, mapParents);
            }

            AutoMapMembers(this, context, mapParents);
        }
Example #2
0
 /// <summary>
 /// Returns <c>true</c> if <paramref name="args.ParameterType"/>:
 /// 1. does not have a parameterless constructor
 /// 2. has a constructor
 /// 3. is not a user defined struct
 /// 4. is not an interface
 /// 5. TypeCode is not an Object.
 /// </summary>
 public static bool ShouldUseConstructorParameters(ShouldUseConstructorParametersArgs args)
 {
     return(!args.ParameterType.HasParameterlessConstructor() &&
            args.ParameterType.HasConstructor() &&
            !args.ParameterType.IsUserDefinedStruct() &&
            !args.ParameterType.IsInterface &&
            Type.GetTypeCode(args.ParameterType) == TypeCode.Object);
 }