/// <summary> /// Load the parser fields with the properties specified in the type. You must use the FieldAttribute and ParseFormatAttribute to specify additional informations like the field length. /// </summary> /// <param name="lineClassType"></param> public LineParser(Type lineClassType) { mFields = Utilities.ExtractFieldListFromType(lineClassType); }
public LineWriter(FieldList fields) { mFields = fields; }
/// <summary> /// Constructor. Fill the Fields list fot specify the columns. /// </summary> public LineParser() { mFields = new FieldList(); }
public static FieldList ExtractFieldListFromType(Type classType) { FieldList fields = new FieldList(); System.Reflection.PropertyInfo[] properties = classType.GetProperties(System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (System.Reflection.PropertyInfo prop in properties) { FieldAttribute fieldAttr = null; ParseFormatAttribute parseFormat = null; object[] attributes = prop.GetCustomAttributes(typeof(FieldAttribute), true); if (attributes.Length > 0) { fieldAttr = (FieldAttribute)attributes[0]; } attributes = prop.GetCustomAttributes(typeof(ParseFormatAttribute), true); if (attributes.Length > 0) { parseFormat = (ParseFormatAttribute)attributes[0]; } object[] valueMappings = prop.GetCustomAttributes(typeof(ValueMappingAttribute), true); object[] standardValues = prop.GetCustomAttributes(typeof(StandardValueAttribute), true); if (fieldAttr != null) { DevAge.ComponentModel.Validator.IValidator validator; if (parseFormat == null) { parseFormat = new ParseFormatAttribute(); } validator = CreateValidator(prop.PropertyType, parseFormat); Field field = new Field(fieldAttr.FieldIndex, prop.Name, fieldAttr.Length, validator); field.TrimBeforeParse = parseFormat.TrimBeforeParse; fields.Add(field); //ValueMapping - to convert specific values, can be an array of attribute, one for each conversion if (valueMappings.Length > 0) { ComponentModel.Validator.ValueMapping mapping = new DevAge.ComponentModel.Validator.ValueMapping(); object[] valList = new object[valueMappings.Length]; object[] strList = new object[valueMappings.Length]; for (int i = 0; i < valueMappings.Length; i++) { //I convert the value assigned to the attribute to ensure that the field is valid and to support DateTime (that cannot be directly assigned to an attribute but must be declared as string) valList[i] = validator.ObjectToValue(((ValueMappingAttribute)valueMappings[i]).FieldValue); strList[i] = ((ValueMappingAttribute)valueMappings[i]).StringValue; } mapping.ThrowErrorIfNotFound = false; mapping.ValueList = valList; mapping.SpecialList = strList; mapping.SpecialType = typeof(string); mapping.BindValidator(validator); } //StandardValues if (standardValues.Length > 0) { object[] valList = new object[standardValues.Length]; for (int i = 0; i < standardValues.Length; i++) { valList[i] = ((StandardValueAttribute)standardValues[i]).StandardValue; } validator.StandardValues = valList; validator.StandardValuesExclusive = true; } } } return(fields); }
public LineWriter(Type lineClassType) { mFields = Utilities.ExtractFieldListFromType(lineClassType); }
public LineWriter(FieldList fields) { mFields = fields; }
public static FieldList ExtractFieldListFromType(Type classType) { FieldList fields = new FieldList(); System.Reflection.PropertyInfo[] properties = classType.GetProperties(System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (System.Reflection.PropertyInfo prop in properties) { FieldAttribute fieldAttr = null; ParseFormatAttribute parseFormat = null; object[] attributes = prop.GetCustomAttributes(typeof(FieldAttribute), true); if (attributes.Length > 0) fieldAttr = (FieldAttribute)attributes[0]; attributes = prop.GetCustomAttributes(typeof(ParseFormatAttribute), true); if (attributes.Length > 0) parseFormat = (ParseFormatAttribute)attributes[0]; object[] valueMappings = prop.GetCustomAttributes(typeof(ValueMappingAttribute), true); object[] standardValues = prop.GetCustomAttributes(typeof(StandardValueAttribute), true); if (fieldAttr != null) { DevAge.ComponentModel.Validator.IValidator validator; if (parseFormat == null) parseFormat = new ParseFormatAttribute(); validator = CreateValidator(prop.PropertyType, parseFormat); Field field = new Field(fieldAttr.FieldIndex, prop.Name, fieldAttr.Length, validator); field.TrimBeforeParse = parseFormat.TrimBeforeParse; fields.Add(field); //ValueMapping - to convert specific values, can be an array of attribute, one for each conversion if (valueMappings.Length > 0) { ComponentModel.Validator.ValueMapping mapping = new DevAge.ComponentModel.Validator.ValueMapping(); object[] valList = new object[valueMappings.Length]; object[] strList = new object[valueMappings.Length]; for (int i = 0; i < valueMappings.Length; i++) { //I convert the value assigned to the attribute to ensure that the field is valid and to support DateTime (that cannot be directly assigned to an attribute but must be declared as string) valList[i] = validator.ObjectToValue( ((ValueMappingAttribute)valueMappings[i]).FieldValue ); strList[i] = ((ValueMappingAttribute)valueMappings[i]).StringValue; } mapping.ThrowErrorIfNotFound = false; mapping.ValueList = valList; mapping.SpecialList = strList; mapping.SpecialType = typeof(string); mapping.BindValidator(validator); } //StandardValues if (standardValues.Length > 0) { object[] valList = new object[standardValues.Length]; for (int i = 0; i < standardValues.Length; i++) { valList[i] = ((StandardValueAttribute)standardValues[i]).StandardValue; } validator.StandardValues = valList; validator.StandardValuesExclusive = true; } } } return fields; }
/// <summary> /// Constructor. Fill the Fields list fot specify the columns. /// </summary> public LineParser() { mFields = new FieldList(); }