static string ToString(object val) { if (val == null) { return("null"); } else if (val is string) { return("\"" + TextWriterTokenWriter.ConvertString((string)val) + "\""); } else if (val is char) { return("'" + TextWriterTokenWriter.ConvertChar((char)val) + "'"); } else { return(val.ToString()); } }
public static string FormatValue(Thread evalThread, Value val) { if (val.IsNull) { return("null"); } else if (val.Type.Kind == TypeKind.Array) { StringBuilder sb = new StringBuilder(); sb.Append(new CSharpAmbience().ConvertType(val.Type)); sb.Append(" {"); bool first = true; int size = val.ArrayLength; for (int i = 0; i < size; i++) { if (!first) { sb.Append(", "); } first = false; sb.Append(FormatValue(evalThread, val.GetElementAtPosition(i))); } sb.Append("}"); return(sb.ToString()); } else if (val.Type.GetAllBaseTypeDefinitions().Any(def => def.IsKnownType(KnownTypeCode.ICollection))) { StringBuilder sb = new StringBuilder(); sb.Append(new CSharpAmbience().ConvertType(val.Type)); sb.Append(" {"); val = val.GetPermanentReference(evalThread); var countProp = val.Type.GetProperties(p => p.Name == "Count" && !p.IsExplicitInterfaceImplementation).Single(); int count = (int)val.GetMemberValue(evalThread, countProp).PrimitiveValue; for (int i = 0; i < count; i++) { if (i > 0) { sb.Append(", "); } var itemProperty = val.Type.GetProperties(p => p.IsIndexer && p.Name == "Item" && !p.IsExplicitInterfaceImplementation).Single(); Value item = val.GetPropertyValue(evalThread, itemProperty, Eval.CreateValue(evalThread, i)); sb.Append(FormatValue(evalThread, item)); } sb.Append("}"); return(sb.ToString()); } else if (val.Type.IsKnownType(KnownTypeCode.Char)) { return("'" + TextWriterTokenWriter.ConvertChar((char)val.PrimitiveValue) + "'"); } else if (val.Type.IsKnownType(KnownTypeCode.String)) { return("\"" + TextWriterTokenWriter.ConvertString((string)val.PrimitiveValue) + "\""); } else if (val.Type.IsPrimitiveType()) { return(TextWriterTokenWriter.PrintPrimitiveValue(val.PrimitiveValue)); } else { return(val.InvokeToString(evalThread)); } }