static public string ObjectToString(object value, ObjectToStringFormatter formatter) { if (value == null) { throw new ArgumentNullException("value"); } StringBuilder toString = new StringBuilder(); // append the class name toString.Append(value.GetType().Name); toString.Append(": "); // append object properties' values string comma = null; foreach (PropertyInfo property in value.GetType().GetProperties()) { ParameterInfo[] indexParameters = property.GetIndexParameters(); if (indexParameters.Length > 0) { continue; } bool skip = false; foreach (Type interfaceType in property.PropertyType.GetInterfaces()) { if (interfaceType == typeof(ICollection)) { skip = true; break; } } if (skip) { continue; } object propertyValue = property.GetValue(value, null); if (propertyValue == null) { continue; } toString.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}{1} = '{2}'", comma, property.Name, propertyValue); comma = ", "; } return(toString.ToString()); }
public static string ObjectToString(object value, ObjectToStringFormatter formatter) { if (value == null) throw new ArgumentNullException ("value"); StringBuilder toString = new StringBuilder (); // append the class name toString.Append (value.GetType ().Name); toString.Append (": "); // append object properties' values string comma = null; foreach (PropertyInfo property in value.GetType ().GetProperties ()) { ParameterInfo[] indexParameters = property.GetIndexParameters (); if (indexParameters.Length > 0) continue; bool skip = false; foreach (Type interfaceType in property.PropertyType.GetInterfaces ()) { if (interfaceType == typeof (ICollection)) { skip = true; break; } } if (skip) continue; object propertyValue = property.GetValue (value, null); if (propertyValue == null) continue; toString.AppendFormat (System.Globalization.CultureInfo.InvariantCulture, "{0}{1} = '{2}'", comma, property.Name, propertyValue); comma = ", "; } return toString.ToString (); }