469,286 Members | 2,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

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 47503
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Roy Chastain | last post: by
reply views Thread by Martin | last post: by
1 post views Thread by johnsonholding | last post: by
7 posts views Thread by Mike J | last post: by
10 posts views Thread by fig000 | last post: by
10 posts views Thread by Derek Hart | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.