473,406 Members | 2,710 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,406 software developers and data experts.

how to loop through properties?

Hi all

Is there any way to do something like this:

SomeClass A = SomeClass(...)
....
foreach (property P in A.Properties)
{
Console.WriteLine(P.Name + "=" + P.Value);
}
In other words, I need to be able to see all object's properties values
(order is not important), i.e. access properties as they were members of a
collection or array.

Thank you
Alex

Jan 13 '06 #1
4 47807
Use Reflection to do this

SomeClass A = SomeClass(...)
PropertyInfo[] properties = A.GetType().GetProperties();

Look on the MSDN for additional info
--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Alex K." <Al***@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
Hi all

Is there any way to do something like this:

SomeClass A = SomeClass(...)
...
foreach (property P in A.Properties)
{
Console.WriteLine(P.Name + "=" + P.Value);
}
In other words, I need to be able to see all object's properties values
(order is not important), i.e. access properties as they were members of a
collection or array.

Thank you
Alex

Jan 13 '06 #2
Alex,

Yes, you can do something like this:

public static void OutputPropertiesToConsole(object o)
{
// Get the type.
Type t = o.GetType();

// Cycle through the properties.
foreach (PropertyInfo p in t.GetProperties())
{
// Write the name and the value.
Console.WriteLine("{0} = {1}", p.Name, p.GetValue(o, null));
}
}

This will get all public properties. You also need to check the code to
see if o is null. If you need private or static properties, you should
adjust the call to GetProperties to include the BindingFlag combinations you
want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alex K." <Al***@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
Hi all

Is there any way to do something like this:

SomeClass A = SomeClass(...)
...
foreach (property P in A.Properties)
{
Console.WriteLine(P.Name + "=" + P.Value);
}
In other words, I need to be able to see all object's properties values
(order is not important), i.e. access properties as they were members of a
collection or array.

Thank you
Alex

Jan 13 '06 #3
Thank you Nicholas
I've just figured it out, and it works.

The only one strange thing though: why should I pass object o as a parameter
to p.GetValue method if p belongs to t extracted from o? Theoretically, p
should have a reference to o already ...

"Nicholas Paldino [.NET/C# MVP]" wrote:
Alex,

Yes, you can do something like this:

public static void OutputPropertiesToConsole(object o)
{
// Get the type.
Type t = o.GetType();

// Cycle through the properties.
foreach (PropertyInfo p in t.GetProperties())
{
// Write the name and the value.
Console.WriteLine("{0} = {1}", p.Name, p.GetValue(o, null));
}
}

This will get all public properties. You also need to check the code to
see if o is null. If you need private or static properties, you should
adjust the call to GetProperties to include the BindingFlag combinations you
want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alex K." <Al***@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
Hi all

Is there any way to do something like this:

SomeClass A = SomeClass(...)
...
foreach (property P in A.Properties)
{
Console.WriteLine(P.Name + "=" + P.Value);
}
In other words, I need to be able to see all object's properties values
(order is not important), i.e. access properties as they were members of a
collection or array.

Thank you
Alex


Jan 13 '06 #4
"Alex K." <Al***@discussions.microsoft.com> a écrit dans le message de news:
3C**********************************@microsoft.com...

| The only one strange thing though: why should I pass object o as a
parameter
| to p.GetValue method if p belongs to t extracted from o? Theoretically, p
| should have a reference to o already ...

Because you are getting the PropertyInfo items from the *type* of the class
that you are working on. Once you have a list of properties from that
*type*, then the GetValue method of the PropertyInfo requires to know which
instance of that type it has to retrieve the value from.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 13 '06 #5

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

Similar topics

9
by: kj | last post by:
Is there any way to programmatically discover and inspect *all* the properties of an object, even those that are not enumerable through a for/in loop? Thanks! kj -- NOTE: In my address...
8
by: deko | last post by:
I'm trying to find a way to set form/control properties programmatically. In a nut shell: 1. Open a database 2. Open a form in design view 3. Do something like this: For Each prp In...
8
by: Roy Chastain | last post by:
I have tried this on a couple of occasions and each time the resulting program goes into a loop and eventually gets a stack overflow. public __gc BaseClass { __property virtual int get_IntValue...
0
by: Martin | last post by:
Hi, Can anybody please tell me (or point me to documentation on) how to loop through all AD properties of a user. The code I have so far is below. Now if I know the property name I can retrieve...
1
by: johnsonholding | last post by:
I have a pop-up flash movie, it works great. Here is the code : <SCRIPT LANGUAGE="JavaScript"> function jumpUp(URL) { window.open(URL, "w"+(new Date()).getTime(),...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
15
by: James Black | last post by:
If you go to http://dante.acomp.usf.edu/HomeworkAssistant/index.php you will see my code. Type in: s = a + b and hit tab, and you will see the extra words. How do I remove these? Here is a...
7
by: Mike J | last post by:
I have a Queston about Properties vs Public / protected ive read alot where people say dont use public or protected members always use Properties.... i kinda dis-agree with that. I do want to...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
10
by: Derek Hart | last post by:
I am going in circles trying to loop through properties on 3rd party controls. For example, I have a textbox that has its maximum length located at MyTextBox.Properties.MaxLength - instead of the...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.