Created
May 21, 2010 17:09
-
-
Save spraints/409104 to your computer and use it in GitHub Desktop.
An alternative to gist: 158795
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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