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