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()); } }
void WriteStringId(BamlContext ctx, ushort id) { string str; if (id > 0x7fff) { str = ctx.KnownThings.Strings((short)-id); } else if (ctx.StringIdMap.ContainsKey(id)) { str = ctx.StringIdMap[id].Value; } else { str = null; } string reference = null; if (str != null) { reference = string.Format("\"{0}\"", TextWriterTokenWriter.ConvertString(str)); } output.WriteReference(string.Format("0x{0:x4}", id), reference, TextTokenType.Number, true); }
public override void WriteInterpolatedText(string text) { output.Write(TextWriterTokenWriter.ConvertString(text)); }
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)); } }
void WriteDefinition(string value, string def = null) { string str = string.Format("\"{0}\"", TextWriterTokenWriter.ConvertString(value)); output.WriteDefinition(str, def ?? IdentifierEscaper.Escape(value), TextTokenType.String, true); }
void WriteString(string value) { string str = string.Format("\"{0}\"", TextWriterTokenWriter.ConvertString(value)); output.Write(str, TextTokenType.String); }