/// <summary> /// Registers the given class map to be used when mapping a row to an object. /// </summary> /// <param name="classMap">The class map to use.</param> public void RegisterClassMap(ExcelClassMap classMap) { if (classMap == null) { throw new ArgumentNullException(nameof(classMap)); } RegisterClassMap(classMap.Type, classMap); }
/// <summary> /// Registers the given class map to be used when mapping a row to an object. /// </summary> /// <param name="classMap">The class map to use.</param> public void RegisterClassMap(ExcelClassMap classMap) { if (classMap == null) { throw new ArgumentNullException(nameof(classMap)); } if (TryGetClassMap(classMap.Type, out ExcelClassMap _)) { throw new ExcelMappingException($"Class map already type \"{classMap.Type.FullName}\""); } ClassMaps.Add(classMap); }
/// <summary> /// Sets the new class map used to map multiple cells in a row to the properties and fields /// of a an object. /// </summary> /// <param name="classMap">The new class map used.</param> /// <returns>The property map that invoked this method.</returns> public ExcelClassMap <T> WithClassMap(ExcelClassMap <T> classMap) { if (classMap == null) { throw new ArgumentNullException(nameof(classMap)); } Properties.Clear(); foreach (ExcelPropertyMap propertyMap in classMap.Properties) { Properties.Add(propertyMap); } return(this); }
/// <summary> /// Gets the class map registered for an object of the given type. /// </summary> /// <param name="classType">The type of the object to get the class map for.</param> /// <param name="classMap">The class map for the given type if it exists, else null.</param> /// <returns>True if a class map exists for the given type, else false.</returns> public bool TryGetClassMap(Type classType, out ExcelClassMap classMap) { if (classType == null) { throw new ArgumentNullException(nameof(classType)); } foreach (ExcelClassMap registeredMap in ClassMaps) { if (registeredMap.Type == classType) { classMap = registeredMap; return(true); } } classMap = null; return(false); }
private void CreateObjectMapGeneric <TProperty>(ExcelPropertyMap propertyMap, MemberExpression memberExpression, Stack <MemberExpression> memberExpressions) { ExcelPropertyMap map = Properties.FirstOrDefault(m => m.Member.Equals(memberExpression.Member)); ExcelClassMap <TProperty> objectPropertyMap; if (map == null) { objectPropertyMap = new ExcelClassMap <TProperty>(); Properties.Add(new ExcelPropertyMap(memberExpression.Member, objectPropertyMap)); } else if (!(map.Map is ExcelClassMap <TProperty> existingMap)) { throw new InvalidOperationException($"Expression is already mapped differently as {map.GetType().ToString()}."); } else { objectPropertyMap = existingMap; } objectPropertyMap.CreateObjectMap(propertyMap, memberExpressions); }
/// <summary> /// Gets the class map registered for an object of the given type. /// </summary> /// <typeparam name="T">The type of the object to get the class map for.</typeparam> /// <param name="classMap">The class map for the given type if it exists, else null.</param> /// <returns>True if a class map exists for the given type, else false.</returns> public bool TryGetClassMap <T>(out ExcelClassMap classMap) => TryGetClassMap(typeof(T), out classMap);