/// <summary> /// eg:04abcd /// data=abcd ,herder=len(abcd)=04 /// </summary> /// <param name="headerLength">add length for eatch data row</param> /// <returns></returns> public string ToFixedLengthString(int headerLength) { int length = 0; StringBuilder sb = new StringBuilder(); foreach (var pi in this.GetType().GetProperties()) { FixedLengthAttribute attribute = FixedLengthHelper.GetCustomAttribute <FixedLengthAttribute>(pi); if (attribute != null && attribute.Length > 0) { if (attribute.Type == FixedLengthType.A) { sb.Append(HandleFixedString(pi.GetValue(this, null), attribute.Length)); } else { sb.Append(HandleFixedNumber(pi.GetValue(this, null), attribute.Length)); } length += attribute.Length; } } if (headerLength > 0) { sb.Insert(0, HandleFixedNumber(length.ToString(), headerLength)); } return(sb.ToString()); }
//public static string DecimalToHex(decimal? input) //{ // return input.HasValue ? (Convert.ToInt32(input.Value)).ToString("X4") : null; //} //public static decimal HexToDecimal(string input) //{ // return Convert.ToDecimal(HexToInt(input)); //} //public static int HexToInt(string input) //{ // return Convert.ToInt32(input, 16); //} //public static string IntToHex(int input, int length) //{ // string pattern = "X"; // if (length > 0) pattern = pattern + length; // return input.ToString(pattern); //} //public static string IntToHex(int input) //{ // return input.ToString("X"); //} //public static decimal IntToDecimal(int input, int precision) //{ // return Convert.ToDecimal(input * 1.0 / (Math.Pow(10, precision))); //} ////public static decimal IntToDecimalByUnit(int input, int precision, int unitId) ////{ //// //HL,HB,HV,SMG 不保留小数,不进位 //// if (unitId == 0 || unitId == 3 || unitId == 4 || unitId == 8) //// return Math.Truncate(IntToDecimal(input, precision)); //// else //// return IntToDecimal(input, precision); ////} //private MaxLengthAttribute GetMaxLengthAttribute(PropertyInfo pi) //{ // object[] cas = pi.GetCustomAttributes(typeof(MaxLengthAttribute), true); // if (cas != null && cas.Length > 0) // { // return (MaxLengthAttribute)cas[0]; // } // return null; //} public static object ParseFixedLengthStringRecursion(string input, Type type) { if (string.IsNullOrEmpty(input)) { return(null); } object obj = Activator.CreateInstance(type); int position = 0; foreach (PropertyInfo pi in type.GetProperties()) { if (pi.Name == "Response") { pi.SetValue(obj, input, null); } FixedLengthAttribute maxLength = GetCustomAttribute <FixedLengthAttribute>(pi); if (maxLength != null && maxLength.Length > 0 && input.Length > position) { string value = ""; if (input.Length >= position + maxLength.Length) { value = input.Substring(position, maxLength.Length); } else { value = input.Substring(position); } if (maxLength.Type == FixedLengthType.X) { pi.SetValue(obj, ParseFixedLengthStringRecursion(value, pi.PropertyType), null); } else { if (pi.PropertyType == typeof(string)) { pi.SetValue(obj, value, null); } else { if (IsNumeric(value)) { pi.SetValue(obj, value, null); } } } position += maxLength.Length; } } return(obj); }
public static T ParseFixedLengthString <T>(T obj, string input) where T : new() { if (string.IsNullOrEmpty(input)) { return(default(T)); } int position = 0; foreach (PropertyInfo pi in typeof(T).GetProperties()) { if (pi.Name == "Response") { pi.SetValue(obj, input, null); } FixedLengthAttribute maxLength = GetCustomAttribute <FixedLengthAttribute>(pi); if (maxLength != null && maxLength.Length > 0 && input.Length > position) { string value = ""; if (input.Length >= position + maxLength.Length) { value = input.Substring(position, maxLength.Length); } else { value = input.Substring(position); } if (pi.PropertyType == typeof(string)) { pi.SetValue(obj, value, null); } else { if (IsNumeric(value)) { pi.SetValue(obj, value, null); } } position += maxLength.Length; } } return(obj); }
public static T FromFixedLengthString <T>(string input, int headerLength) { T obj = Activator.CreateInstance <T>(); string value = ""; int startIndex = 0; if (headerLength > 0) { startIndex = headerLength; } StringBuilder sb = new StringBuilder(); foreach (var pi in obj.GetType().GetProperties()) { FixedLengthAttribute attribute = FixedLengthHelper.GetCustomAttribute <FixedLengthAttribute>(pi); if (attribute != null && attribute.Length > 0) { if (input.Length <= startIndex) { break; } else if (input.Length > startIndex && input.Length < startIndex + attribute.Length) { //value = input.Substring(startIndex); value = SubstringWide(input, startIndex); } else { //value = input.Substring(startIndex, attribute.Length); value = SubstringWide(input, startIndex, attribute.Length); } pi.SetValue(obj, value, null); startIndex += attribute.Length; } } return(obj); }