Skip to content

Instantly share code, notes, and snippets.

@spraints
Created May 21, 2010 17:09
Show Gist options
  • Select an option

  • Save spraints/409104 to your computer and use it in GitHub Desktop.

Select an option

Save spraints/409104 to your computer and use it in GitHub Desktop.
An alternative to gist: 158795
// an alternative to http://gist.github.com/158795
// note: this version doesn't deal with cycles at all.
private string Inspect(object x)
{
return TryNullInspect(x)
?? TryInspect<string>(x, s => "\"" + s.Replace("\"", "\\\""))
?? TryInspect<DictionaryEntry>(x, e => "{ " + e.Key + " => " + Inspect(e.Value) + " }")
?? TryInspect<IEnumerable>(x, e => "[ " + String.Join(", ", e.Cast<object>().Select(Inspect).ToArray()) + " ]")
?? x.ToString();
}
private string TryNullInspect(object x)
{
return x == null ? "null" : null;
}
private string TryInspect<T>(object x, Func<T, string> inspect)
{
return x is T ? inspect((T)x) : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment