Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
## "Evil" ToString Extension method that handles Nullable<> types public static string ToString(this object anObject, string aFormat) { return ToString(anObject, aFormat, null); } public static string ToString(this object anObject, string aFormat, IFormatProvider formatProvider) { var sb = new StringBuilder(); var type = anObject.GetType(); var reg = new Regex(@"({)([^}]+)(})", RegexOptions.IgnoreCase); MatchCollection mc = reg.Matches(aFormat); int startIndex = 0; foreach (Match m in mc) { var g = m.Groups[2]; //it's second in the match between { and } int length = g.Index - startIndex - 1; sb.Append(aFormat.Substring(startIndex, length)); string toGet; var toFormat = String.Empty; int formatIndex = g.Value.IndexOf(":"); //formatting would be to the right of a : if (formatIndex == -1) //no formatting, no worries { toGet = g.Value; } else //pickup the formatting { toGet = g.Value.Substring(0, formatIndex); toFormat = g.Value.Substring(formatIndex + 1); } //first try properties PropertyInfo retrievedProperty = type.GetProperty(toGet); Type retrievedType = null; object retrievedObject = null; if (retrievedProperty != null) { retrievedType = retrievedProperty.PropertyType; retrievedObject = retrievedProperty.GetValue(anObject, null); } else //try fields { FieldInfo retrievedField = type.GetField(toGet); if (retrievedField != null) { retrievedType = retrievedField.FieldType; retrievedObject = retrievedField.GetValue(anObject); } } if (retrievedType != null) //Cool, we found something { string result; if (toFormat == String.Empty) //no format info { result = retrievedObject.ToString(); } else //format info { if (retrievedType.IsNullableType()) { // Get the underlying type of the object retrievedType = Nullable.GetUnderlyingType(retrievedType); } result = retrievedType.InvokeMember("ToString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.IgnoreCase, null, retrievedObject, new object[] {toFormat, formatProvider}) as string; } sb.Append(result); } else //didn't find a property with that name, so be gracious and put it back { sb.Append("{"); sb.Append(g.Value); sb.Append("}"); } startIndex = g.Index + g.Length + 1; } if (startIndex < aFormat.Length) //include the rest (end) of the string { sb.Append(aFormat.Substring(startIndex)); } return sb.ToString(); } public static bool IsNullableType( this Type theType ) { return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof (Nullable<>))); }
This paste will be private.
From the Design Piracy series on my blog: