public static object Format(object value) { // If it's null, return a known string for null values if (value == null) { return(NullInstance); } // Give another partial implementation a chance to provide its own string representation string result = null; AdditionalCustomizedToString(value, ref result); if (result != null) { return(result); } // Format arrays with their element type name and length Array arr = value as Array; if (arr != null) { return($"{arr.GetType().GetElementType()}[{((Array)value).Length}]"); } // Format ICollections as the name and count ICollection c = value as ICollection; if (c != null) { return($"{c.GetType().Name}({c.Count})"); } // Format SafeHandles as their type, hash code, and pointer value SafeHandle handle = value as SafeHandle; if (handle != null) { return($"{handle.GetType().Name}:{handle.GetHashCode()}(0x{handle.DangerousGetHandle():X})"); } // Format IntPtrs as hex if (value is IntPtr) { return($"0x{value:X}"); } // If the string representation of the instance would just be its type name, // use its id instead. string toString = value.ToString(); if (toString == null || toString == value.GetType().FullName) { return(IdOf(value)); } // Otherwise, return the original object so that the caller does default formatting. return(value); }