static public string Build(System.Globalization.CultureInfo ct, params System.Object[] values) { System.Text.StringBuilder sb = new System.Text.StringBuilder(64); string overrideprefix = string.Empty; for (int i = 0; i < values.Length;) { Object first = values[i]; if (first is NewPrefix) // first item is special, a new prefix, override { overrideprefix = (first as NewPrefix).prefix; i++; } else if (first is string) // normal, string { System.Diagnostics.Debug.Assert(i + 2 <= values.Length); string[] fieldnames = ((string)first).Split(';'); object value = values[i + 1]; i += 2; string pad = ", "; if (fieldnames[0].Length > 0 && fieldnames[0][0] == '<') { fieldnames[0] = fieldnames[0].Substring(1); pad = ""; } if (value != null) { if (value is bool) { string s = ((bool)value) ? fieldnames[1] : fieldnames[0]; sb.AppendPrePad(s, (overrideprefix.Length > 0) ? overrideprefix : pad); overrideprefix = string.Empty; } else { string format = fieldnames.Length >= 3 ? fieldnames[2] : "0"; string output; if (value is string) { output = (string)value; } else if (value is double) { output = ((double)value).ToString(format, ct); } else if (value is float) { output = ((float)value).ToString(format, ct); } else if (value is int) { output = ((int)value).ToString(format, ct); } else if (value is long) { output = ((long)value).ToString(format, ct); } else if (value is double?) { output = ((double?)value).Value.ToString(format, ct); } else if (value is float?) { output = ((float?)value).Value.ToString(format, ct); } else if (value is int?) { output = ((int?)value).Value.ToString(format, ct); } else if (value is long?) { output = ((long?)value).Value.ToString(format, ct); } else if (value is DateTime) { format = fieldnames.Length >= 3 ? fieldnames[2] : "g"; output = ((DateTime)value).ToString(format, ct); } else { Type t = value.GetType(); if (t.BaseType.Name.Equals("Enum")) { var ev = Activator.CreateInstance(t); ev = value; output = ev.ToString(); } else { output = ""; System.Diagnostics.Debug.Assert(false); } } // if printed something, text must be non null and of length, and it returns true. Only adds on prefix and prepad if required if (sb.AppendPrePad(output, fieldnames[0], (overrideprefix.Length > 0) ? overrideprefix : pad)) { // prefix with fieldnames[0], and prefix with newline if defined, or pad if (fieldnames.Length >= 2 && fieldnames[1].Length > 0) { sb.Append(fieldnames[1]); } overrideprefix = string.Empty; } } } } else { System.Diagnostics.Debug.Assert(false); } } return(sb.ToString()); }
// first object = format // second object = data value // if data value null or empty, not printed // if data value is bool, format = false text;true text // else format is prefix;postfix;[doubleformat] value // if prefix starts with a <, no ,<spc> pad static public string FieldBuilder(params System.Object[] values) { System.Text.StringBuilder sb = new System.Text.StringBuilder(64); for (int i = 0; i < values.Length;) { System.Diagnostics.Debug.Assert(i + 2 <= values.Length); string[] fieldnames = ((string)values[i]).Split(';'); object value = values[i + 1]; i += 2; string pad = ", "; if (fieldnames[0].Length > 0 && fieldnames[0][0] == '<') { fieldnames[0] = fieldnames[0].Substring(1); pad = ""; } if (value != null) { if (value is bool) { string s = ((bool)value) ? fieldnames[1] : fieldnames[0]; sb.AppendPrePad(s, pad); } else if (value is string) { string text = (string)value; if (sb.AppendPrePad(text, fieldnames[0], pad)) // if printed something, text must be non null and of length { if (fieldnames.Length >= 2 && fieldnames[1].Length > 0) { sb.Append(fieldnames[1]); } } } else { string output; if (value is double) { System.Diagnostics.Debug.Assert(fieldnames.Length >= 3); output = ((double)value).ToString(fieldnames[2]); } else if (value is int) { output = ((int)value).ToString(System.Globalization.CultureInfo.InvariantCulture); } else if (value is long) { output = ((long)value).ToString(System.Globalization.CultureInfo.InvariantCulture); } else if (value is double?) { System.Diagnostics.Debug.Assert(fieldnames.Length >= 3); output = ((double?)value).Value.ToString(fieldnames[2]); } else if (value is int?) { output = ((int?)value).Value.ToString(System.Globalization.CultureInfo.InvariantCulture); } else if (value is long?) { output = ((long?)value).Value.ToString(System.Globalization.CultureInfo.InvariantCulture); } else if (value is DateTime) { output = ((DateTime)value).ToString(); } else { output = ""; System.Diagnostics.Debug.Assert(false); } if (sb.AppendPrePad(output, fieldnames[0], pad)) // if printed something, text must be non null and of length { if (fieldnames.Length >= 2 && fieldnames[1].Length > 0) { sb.Append(fieldnames[1]); } } } } } return(sb.ToString()); }