/// <summary>
        /// Initializes a new instance of the <see cref="CsvPropertyReferenceMap"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="property">The property.</param>
        public CsvPropertyReferenceMap( Type type, PropertyInfo property )
        {
            if( type != typeof( CsvClassMap ) )
            {
                throw new ArgumentException( "The type is not a CsvClassMap." );
            }

            this.property = property;
            var map = (CsvClassMap)Activator.CreateInstance(type);
            referenceProperties = map.PropertyMaps;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the action for an object.
        /// </summary>
        /// <param name="type">The type of object to create the action for.</param>
        protected virtual void CreateActionForObject(Type type)
        {
            var recordParameter = Expression.Parameter(type, "record");

            // Get a list of all the properties so they will
            // be sorted properly.
            var properties = new CsvPropertyMapCollection();

            AddProperties(properties, configuration.Maps[type]);

            if (properties.Count == 0)
            {
                throw new CsvWriterException(string.Format("No properties are mapped for type '{0}'.", type.FullName));
            }

            var delegates = new List <Delegate>();

            foreach (var propertyMap in properties)
            {
                if (!CanWrite(propertyMap))
                {
                    continue;
                }

                if (propertyMap.Data.TypeConverter == null || !propertyMap.Data.TypeConverter.CanConvertTo(typeof(string)))
                {
                    // Skip if the type isn't convertible.
                    continue;
                }

                // Find the object that contains this property.
                var currentRecordObject = CreateParameterForProperty(recordParameter, configuration.Maps[type], propertyMap);

                Expression fieldExpression = Expression.Property(currentRecordObject, propertyMap.Data.Property);

                var typeConverterExpression = Expression.Constant(propertyMap.Data.TypeConverter);
                if (propertyMap.Data.TypeConverterOptions.CultureInfo == null)
                {
                    propertyMap.Data.TypeConverterOptions.CultureInfo = configuration.CultureInfo;
                }
                var typeConverterOptions = Expression.Constant(propertyMap.Data.TypeConverterOptions);
                var method = propertyMap.Data.TypeConverter.GetType().GetMethod("ConvertToString");
                fieldExpression = Expression.Convert(fieldExpression, typeof(object));
                fieldExpression = Expression.Call(typeConverterExpression, method, typeConverterOptions, fieldExpression);

#if !WINRT_4_5
                if (type.IsClass)
#else
                if (type.GetTypeInfo().IsClass)
#endif
                {
                    var areEqualExpression = Expression.Equal(recordParameter, Expression.Constant(null));
                    fieldExpression = Expression.Condition(areEqualExpression, Expression.Constant(string.Empty), fieldExpression);
                }

                var writeFieldMethodCall = Expression.Call(Expression.Constant(this), "WriteField", new[] { typeof(string) }, fieldExpression);

                var actionType = typeof(Action <>).MakeGenericType(type);
                delegates.Add(Expression.Lambda(actionType, writeFieldMethodCall, recordParameter).Compile());
            }

            typeActions[type] = CombineDelegates(delegates);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates the write record action for the given type if it
        /// doesn't already exist.
        /// </summary>
        /// <param name="type">The type of the custom class being written.</param>
        /// <param name="expressionCompiler">The expression compiler.</param>
        protected virtual void CreateWriteRecordAction(Type type)
        {
            if (typeActions.ContainsKey(type))
            {
                return;
            }

            var recordParameter = Expression.Parameter(type, "record");

            if (configuration.Maps[type] == null)
            {
                // We need to check again in case the header was not written.
                configuration.Maps.Add(configuration.AutoMap(type));
            }

            // Get a list of all the properties so they will
            // be sorted properly.
            var properties = new CsvPropertyMapCollection();

            AddProperties(properties, configuration.Maps[type]);

            var delegates = new List <Delegate>();

            foreach (var propertyMap in properties)
            {
                if (!CanWrite(propertyMap))
                {
                    continue;
                }

                if (string.IsNullOrEmpty(propertyMap.FormatValue) && (propertyMap.TypeConverterValue == null || !propertyMap.TypeConverterValue.CanConvertTo(typeof(string))))
                {
                    // Skip if the type isn't convertible.
                    continue;
                }

                // Find the object that contains this property.
                var currentRecordObject = CreateParameterForProperty(recordParameter, configuration.Maps[type], propertyMap);

                Expression fieldExpression = Expression.Property(currentRecordObject, propertyMap.PropertyValue);

                if (!string.IsNullOrEmpty(propertyMap.FormatValue))
                {
                    // Use string.Format instead of TypeConverter.
                    var formatExpression = Expression.Constant(propertyMap.FormatValue);
                    var method           = typeof(string).GetMethod("Format", new[] { typeof(IFormatProvider), typeof(string), typeof(object[]) });
                    fieldExpression = Expression.Convert(fieldExpression, typeof(object));
                    fieldExpression = Expression.NewArrayInit(typeof(object), fieldExpression);
                    fieldExpression = Expression.Call(method, Expression.Constant(Configuration.CultureInfo), formatExpression, fieldExpression);
                }
                else
                {
                    var typeConverterExpression = Expression.Constant(propertyMap.TypeConverterValue);
                    var method = propertyMap.TypeConverterValue.GetType().GetMethod("ConvertToString", new[] { typeof(CultureInfo), typeof(object) });
                    fieldExpression = Expression.Convert(fieldExpression, typeof(object));
                    fieldExpression = Expression.Call(typeConverterExpression, method, Expression.Constant(Configuration.CultureInfo), fieldExpression);
                }

                var areEqualExpression = Expression.Equal(recordParameter, Expression.Constant(null));
                fieldExpression = Expression.Condition(areEqualExpression, Expression.Constant(string.Empty), fieldExpression);

                var writeFieldMethodCall = Expression.Call(Expression.Constant(this), "WriteField", new[] { typeof(string) }, fieldExpression);

                var actionType = typeof(Action <>).MakeGenericType(type);
                delegates.Add(Expression.Lambda(actionType, writeFieldMethodCall, recordParameter).Compile());
            }

            typeActions[type] = CombineDelegates(delegates);
        }