/// <summary> /// Gets a date as a string, applying the appropriate formatting /// specified in the text file mapping attribute, if specified /// </summary> /// <param name="value">DateTime value to format</param> /// <param name="mappingAttribute">Mapping attribute with formatting information</param> /// <returns>Formatted DateTime string</returns> public static string GetDateAsString(DateTime value, TextFileMappingAttribute mappingAttribute) { if (mappingAttribute.DateTimeFormat != null) { return(value.ToString(mappingAttribute.DateTimeFormat)); } return(value.ToString()); }
/// <summary> /// Retrieve a converter for the specified property /// </summary> /// <param name="info">Property info</param> /// <param name="attribute">Tax mapping attribute</param> /// <returns>An IConvertAndSet instance that will provide string conversion for the specified property</returns> public static IConvertAndSet Get(PropertyInfo info, TextFileMappingAttribute attribute) { Type converterType; if (_converters.TryGetValue(info.PropertyType, out converterType)) { var converter = (IConvertAndSet)Activator.CreateInstance(converterType); converter.Setup(info, attribute); return(converter); } throw new NotImplementedException($"No converter available for target type {info.PropertyType} which is required for target property \"{info.Name}\""); }
/// <summary> /// Parse a string value to a DateTime, using formatting /// hints provided in the TextFileMappingAttribute associated /// with the property, if provided. /// </summary> /// <param name="value">String value to parse to a DateTime</param> /// <param name="mappingAttribute">Mapping attribute for the property</param> /// <returns>DateTime parsed from value</returns> public static DateTime ParseDate(string value, TextFileMappingAttribute mappingAttribute) { DateTime parsedDate = DateTime.MinValue; if (mappingAttribute.DateTimeFormat != null) { parsedDate = DateTime.ParseExact(value, mappingAttribute.DateTimeFormat, null); } else { parsedDate = DateTime.Parse(value); } return(parsedDate); }
/// <summary> /// Setup the converter, assigning it to a property and /// providing additional mapping information /// </summary> /// <param name="propertyInfo">Property this converter is assigned to</param> /// <param name="mapper">Additional mapping information</param> public virtual void Setup(PropertyInfo propertyInfo, TextFileMappingAttribute mapper) { this.MappingAttribute = mapper; this.Setter = (Action <TRow, TTarget>)propertyInfo.GetSetMethod().CreateDelegate(typeof(Action <TRow, TTarget>)); this.Getter = (Func <TRow, TTarget>)propertyInfo.GetGetMethod().CreateDelegate(typeof(Func <TRow, TTarget>)); }