/// <summary> /// Maps a property to a CSV field. /// </summary> /// <param name="expression">The property to map.</param> /// <returns>The property mapping.</returns> public virtual CsvPropertyMap Map(Expression <Func <T, object> > expression) { var stack = ReflectionHelper.GetProperties(expression); if (stack.Count == 0) { throw new InvalidOperationException("No properties were found in expression '{expression}'."); } CsvClassMap currentClassMap = this; PropertyInfo property; if (stack.Count > 1) { // We need to add a reference map for every sub property. while (stack.Count > 1) { property = stack.Pop(); var mapType = typeof(DefaultCsvClassMap <>).MakeGenericType(property.PropertyType); var referenceMap = currentClassMap.References(mapType, property); currentClassMap = referenceMap.Data.Mapping; } } // Add the property map to the last reference map. property = stack.Pop(); return(currentClassMap.Map(property)); }
/// <summary> /// Maps a property/field to a CSV field. /// </summary> /// <param name="expression">The property/field to map.</param> /// <param name="useExistingMap">If true, an existing map will be used if available. /// If false, a new map is created for the same property/field.</param> /// <returns>The property/field mapping.</returns> public virtual CsvPropertyMap Map(Expression <Func <T, object> > expression, bool useExistingMap = true) { var stack = ReflectionHelper.GetMembers(expression); if (stack.Count == 0) { throw new InvalidOperationException("No properties/fields were found in expression '{expression}'."); } CsvClassMap currentClassMap = this; MemberInfo member; if (stack.Count > 1) { // We need to add a reference map for every sub property/field. while (stack.Count > 1) { member = stack.Pop(); Type mapType; var property = member as PropertyInfo; var field = member as FieldInfo; if (property != null) { mapType = typeof(DefaultCsvClassMap <>).MakeGenericType(property.PropertyType); } else if (field != null) { mapType = typeof(DefaultCsvClassMap <>).MakeGenericType(field.FieldType); } else { throw new InvalidOperationException("The given expression was not a property or a field."); } var referenceMap = currentClassMap.References(mapType, member); currentClassMap = referenceMap.Data.Mapping; } } // Add the property/field map to the last reference map. member = stack.Pop(); return(currentClassMap.Map(member, useExistingMap)); }
#pragma warning disable CS0693 // Type parameter has the same name as the type parameter from outer type public IHasMapOptions <TClass, TProperty> Map <TProperty>(Expression <Func <TClass, TProperty> > expression, bool useExistingMap = true) { return(new PropertyMapBuilder <TClass, TProperty>(classMap, classMap.Map(expression, useExistingMap))); }