how to loop through properties?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Use Reflection to do this

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

Look on the MSDN for additional info
 
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.
 
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 said:
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]
- (e-mail address removed)

Alex K. said:
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
 
"Alex K." <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
Back
Top