public override string ToLine(char separator) { var sb = new StringBuilder(); var propertyInfos = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var properties = new List <PropertyInfo>(propertyInfos); var comparer = new FieldAttributeComparer(); properties.Sort(comparer); foreach (PropertyInfo propertyInfo in properties) { var fieldAttribute = propertyInfo.GetCustomAttributes(typeof(FieldAttribute), false).FirstOrDefault() as FieldAttribute; if (fieldAttribute != null) { var propertyValue = propertyInfo.GetValue(this, null) ?? string.Empty; sb.AppendFormat("{0}{1}", propertyValue, separator); } } return(sb.ToString()); }
public override bool Load(string csvRecordString, char separator) { if (csvRecordString == null) { return(false); } var values = csvRecordString.Split(separator); var propertyInfos = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var properties = new List <PropertyInfo>(propertyInfos); var comparer = new FieldAttributeComparer(); properties.Sort(comparer); foreach (PropertyInfo propertyInfo in properties) { var fieldAttribute = propertyInfo.GetCustomAttributes(typeof(FieldAttribute), false).FirstOrDefault() as FieldAttribute; if (fieldAttribute != null) { propertyInfo.SetValue(this, values[fieldAttribute.Order], null); } } return(true); }