3

I have an application that is implementing its own C# console (via Roslyn's scripting engine). I already got the code to execute a statement and got the possible return value and now I would like to output a "nice" string to the console. The trivial approach to call "ToString()" will usually have very human unreadable output.

The best solution for me would be to have the same kind of inspection output that the "Immediate Window" of Visual Studio uses.

So for example if my statement evaluates to a new string[]{"asd"}, then just calling ToString() would give me

System.String[]

where I would like to have the equivalent output of the Immediate Window - something like this:

{string[1]}
    [0]: "asd"

So does anyone knows how to call the same convertion that the Immediate Window uses?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Imi
  • 1,579
  • 1
  • 12
  • 22
  • 2
    the closest I got to achieving this was using an "object dumper" as described [here](http://stackoverflow.com/questions/360277/what-is-the-best-way-to-dump-entire-objects-to-a-log-in-c) or [here](http://stackoverflow.com/questions/1347375/c-sharp-object-dumper) – Cristian Lupascu May 29 '13 at 09:00
  • Thank you, very interesting. I will leave this question a bit open in the hope of finding an API access to the actual immediate window convertion (because my users will also use Visual Studio, so that would really be the best and "most familiar" output), but these links surely containt good examples of "human-readable-dumping". – Imi May 29 '13 at 09:36

2 Answers2

1

It appears you have an array of Strings containing a single String, and you call .ToString() against the array. Try to replace

.ToString()

with

[0].ToString()

and see if that would give you the expected result.

Daniel
  • 763
  • 1
  • 8
  • 25
  • Using a string array was just a quick example to show the nice output of the immediate window vs. the standard implementation of ToString(). The code I have in mind should work with every kind of object. – Imi May 29 '13 at 09:05
  • Please give an actual example - what kind of object are you calling .ToString() against? Is it a class you defined on your own? In that case you have to override the ToString() method inside that class and return the desired text. – Daniel May 29 '13 at 09:25
  • I can not give an actual example because it is a generic console that should look nice with ALL kinds of objects (or at least "most"). Any specific example (like the string[] above) distracts from the problem of *generic* displaying an unknown object. I cannot implement any method on the target object either, because I do not control the class (and the class probably does not even exist anyway at the time I write the console code). Of course, I can write my own display code using reflection and tests for standard types, but my question is about an build-in method I could reuse. – Imi May 29 '13 at 09:33
  • In that case it is correct as it is. You may include some generic considerations, like showing different elements for an array (if so-my answer shows you how), BUT other than that what is displayed by .ToString() depends on the person who wrote the object you're inspecting, and you shouldn't override his decision on what the object should return when ToString() is called. – Daniel May 29 '13 at 10:34
1

In addition to previous comments/answers, you can use any kind of serializer that outputs a human readable value. I have used Json.NET for this:

string humanReadable = JsonConvert.SerializeObject(someObj, Formatting.Indented);

Nuget package: http://nuget.org/packages/Newtonsoft.Json/

lightbricko
  • 2,649
  • 15
  • 21