views:

1846

answers:

8

Is there a method built in to .NET that can write all the properties and such of an object to the console? Could make one using reflection of course, but I'm curious to if this already exists... especially since you can do it in Visual Studio in the Immediate Window. There you can an object name (while in debug mode), press enter, and it is printed fairly prettily with all its stuff.

Does a method like this exist?

A: 

Don't think so. I've always had to write them or use someone else's work to get that info. Has to be reflection as far as i'm aware.

EDIT:
Check this out. I was investigating some debugging on long object graphs and noticed this when i Add Watches, VS throws in this class: Mscorlib_CollectionDebugView<>. It's an internal type for displaying collections nicely for viewing in the watch windows/code debug modes. Now coz it's internal you can reference it, but u can use Reflector to copy (from mscorlib) the code and have your own (the link above has a copy/paste example). Looks really useful.

cottsak
+2  A: 

Maybe via JavaScriptSerializer.Serialize?

Marc Gravell
Interesting... how would you use that?
Svish
You'd need to tidy the JSON blob up so its presentable.. and i'd say it would take as many lines doing that as writing your own reflection code. But that's my 2c.
cottsak
good point cottsak. figured out how to use it now, and allthough all the data seems to be there, it was not very readable out of the box =)
Svish
+4  A: 

The ObjectDumper class has been known to do that. I've never confirmed, but I've always suspected that the immediate window uses that.

EDIT: I just realized, that the code for ObjectDumper is actually on your machine. Go to:

c:/Program Files/Microsoft Visual Studio 9.0/Samples/1033/CSharpSamples.zip

This will unzip to a folder called LinqSamples. In there, there's a project called ObjectDumper. Use that.

( This will also make David in the comments happy :) )

BFree
Yay to deep-linking into some project without obvious license. Double-yay to the "catch{}" used when dumping members. Still +1 for a nice example of how it can be done.
David Schmitt
Woah, that totally worked. All though some depth control would have been nice to have, haha. Thanks for great tip! =)
Svish
See my edit. The one in the samples actually has an overload that takes depth.
BFree
Hm, is it just me, or is this outputting everything on a single line?
Svish
Hm, no it is outputting each object on one line it seems... So all the properties come on one line. Hmm...
Svish
+1  A: 

This is exactly what reflection is for. I don't think there's a simpler solution, but reflection isn't that code intensive anyway.

Jon B
A: 

Following snippet will do the desired function:

Type t = obj.GetType();//where obj is object whose properties you need.
PropertyInfo [] pi =t.GetProperties();
foreach (PropertyInfo p in pi)
{
    System.Console.WriteLine(p.Name + "    " + p.GetType);
}

I think if you write this as extension method you could use it on all type of objects.

TheVillageIdiot
This wouldn't handle objects consisting of other objects though. It also does not output the values of the properties. Only the names. And I already know those :P
Svish
A: 

Any other solution/library is in the end going to use reflection to introspect the type...

mP
Of course, but I would still like to not code it myself if I don't have to ;)
Svish
+4  A: 

You can use the TypeDescriptor class to do this:

foreach(PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
{
    string name=descriptor.Name;
    object value=descriptor.GetValue(obj);
    Console.WriteLine("{0}={1}",name,value);
}

TypeDescriptor lives in the System.ComponentModel namespace and is the API that Visual Studio uses to display your object in its property browser. It's ultimately based on reflection (as any solution would be), but it provides a pretty good level of abstraction from the reflection API.

Sean
Cool! Didn't know about that. How is using this PropertyDescriptor and GetValue, compared to using obj.GetType().GetProperties() and GetValue and SetValue? Is it kind of the same just a different "interface"?
Svish
It's a high level API over the reflection API. It's geared towards displaying properties in a user-friendly manner. The PropertyDescriptor class has various methods to allow you to easily edit, change and reset the property value, if you wan to.
Sean
+1  A: 

Regarding TypeDescriptor from Sean's reply (I can't comment because I have a bad reputation)... one advantage to using TypeDescriptor over GetProperties() is that TypeDescriptor has a mechanism for dynamically attaching properties to objects at runtime and normal reflection will miss these.

For example, when working with PowerShell's PSObject, which can have properties and methods added at runtime, they implemented a custom TypeDescriptor which merges these members in with the standard member set. By using TypeDescriptor, your code doesn't need to be aware of that fact.

Components, controls, and I think maybe DataSets also make use of this API.

Josh Einstein
Cool! Thanks for info =)
Svish