473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Listing properties of an object

Hi

Is there an easy or a built-in way to collect all properties of an object
into a string variable for debugging purpose? I am looking for something
similar to this;

"Property1: Property1value
Property2: Property2value
Property3: Property3value
.....
Propertyn: Propertynvalue"

Thanks

Regards
May 3 '07 #1
4 977
Hi,

John wrote:
Is there an easy or a built-in way to collect all properties of an object
into a string variable for debugging purpose?
I don't think there is. If you're very up to date and using LINQ already,
you could use the ObjectDumper that comes with it as a tool. Otherwise,
this article may be helpful to you:

http://www.codeproject.com/csharp/debugwriter.asp
Oliver Sturm
--
http://www.sturmnet.org/blog - MVP C#
May 3 '07 #2
KJ
Check this out:

http://msdn2.microsoft.com/en-us/library/ms950764.aspx

On May 3, 12:08 pm, "John" <J...@nospam.infovis.co.ukwrote:
Hi

Is there an easy or a built-in way to collect all properties of an object
into a string variable for debugging purpose? I am looking for something
similar to this;

"Property1: Property1value
Property2: Property2value
Property3: Property3value
....
Propertyn: Propertynvalue"

Thanks

Regards

May 3 '07 #3
John wrote:
Is there an easy or a built-in way to collect all properties of an object
into a string variable for debugging purpose?
My preference is to create a ToString() method for each class that tells
me "enough" about the object that I can see what's going on (or, at the
very least, to identify which /instance/ of the object I'm looking at!).

Public Overrides Function ToString()
Return Me.X & ", " & Me.Y
End Function

Of course, there's nothing to stop you adding a "debugging" overload as
well ...

Public Overrides Overloads Function ToString(ByVal debug as Boolean)
If debug Then
Dim sDebugData as String _
= ... lots of stuff ...
Else
Return Me.ToString()
End If
End Function

HTH,
Phill W.
May 4 '07 #4
Simplest way I believe is:

Type t = f.GetType();

PropertyInfo[] props = t.GetProperties();

foreach (PropertyInfo pi in props) {

Console.WriteLine("Property {0}, value {1}", pi.Name, pi.GetValue(f, null));

}
Here is sample of produced output for a form (f = new Form();)

....

Property BackgroundImage, value
Property BackgroundImageLayout, value Tile
Property Bottom, value 322
Property Bounds, value {X=22,Y=22,Width=300,Height=300}
Property CanFocus, value False
Property CanSelect, value False
Property Capture, value False
Property CausesValidation, value True
Property ClientRectangle, value {X=0,Y=0,Width=292,Height=273}
Property CompanyName, value Microsoft Corporation
Property ContainsFocus, value False
Property ContextMenu, value
Property ContextMenuStrip, value
Property Controls, value System.Windows.Forms.Form+ControlCollection
Property Created, value False
Property Cursor, value [Cursor: Default]
Property DataBindings, value System.Windows.Forms.ControlBindingsCollection

....

You'll might need to tweak BindingFlags and indexes to get to arrays and
special cases, however this should do the desired in most cases

Have fun
Alex
"John" <Jo**@nospam.infovis.co.ukwrote in message
news:Om**************@TK2MSFTNGP06.phx.gbl...
Hi

Is there an easy or a built-in way to collect all properties of an object
into a string variable for debugging purpose? I am looking for something
similar to this;

"Property1: Property1value
Property2: Property2value
Property3: Property3value
....
Propertyn: Propertynvalue"

Thanks

Regards

May 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bengt Richter | last post by:
Still looking for a unifying concept ;-) It struck me that @deco1 @deco2 def foo():pass uses '@' to register at-next-name-binding processing in a way similar to atexit's registering of...
3
by: Jason | last post by:
I need to get a listing of users from Active Directory. Getting the list is no problem using the DirectorySearcher class. My problem is getting the full legacy-style domain name of the user (in the...
4
by: jbesr1230 | last post by:
Hello, How would I print out a listing of the queries that are in a database? I would like to have a spreadsheet style listing with the columns of: Query Name, Description, Modified Date, Created...
2
by: Tom | last post by:
I need to get a directory listing through http. If I put the directory path in the browser address bar such as http://somewebpage.com/subdir I get the listing of the directory. Of course this is...
2
by: Stu | last post by:
Hi, I have a class with quite a large number of properties that stores booking information.. I need to create an audit trail for these bookings every time something is changed. I thought about...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
6
by: Une bévue | last post by:
i'd like to know objects properties and/or methods. saying i do have an object "o" being part of the dom (a div or an ul...) how could i list (introspection) all the properties and methods...
4
by: John | last post by:
Hi Is there an easy or a built-in way to collect all properties of an object into a string variable for debugging purpose? I am looking for something similar to this; "Property1:...
3
by: =?Utf-8?B?ZG1idXNv?= | last post by:
I would like a listing of all users in the current domain (or on my computer). I'm running Windows XP Home Edition and I've set up four user, one for each member of my family. 'My.User.Name'...
5
by: Nathan Sokalski | last post by:
I am looking for a reference that lists the equivelant JavaScript/JScript properties (for example, it took me a while to find that event.x/event.y were the IE equivelant of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.