By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,633 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,633 IT Pros & Developers. It's quick & easy.

How to traverse an object properties?

P: n/a
Hi, i've been trying to investigate how to traverse through an object
instance properties or fields with no success. I have read about
IEnumerable interface but that seems to apply only to collection of
objects. What i want to do actually is to traverse through each
property of an instance and if possible, assign a null value...
something like this:

MyClass instance = new MyClass();

instance.property1 = "A string";
instance.property2 = 21;

then, once i have assigned values, i would like to be able to do
something like this:

foreach (object obj in myinstance)
{
obj = null;
}

What i meant above with object is that of course i may have an int, a
string, etc. so i dont know what type of data i may get and so i want
to traverse the properties despite its type. I dont know if this is
possible, i've read a lot that implementing IEnumerable interfaces it
can be donde but i just cannot find any real example. I also found that
using generics i can handle the value type thing.

Any help would be really appreciated, a URL with an example would be
great :)

Thanks.

Dec 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"visor7" <gu***********@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
Hi, i've been trying to investigate how to traverse through an object
instance properties or fields with no success. I have read about
IEnumerable interface but that seems to apply only to collection of
objects. What i want to do actually is to traverse through each
property of an instance and if possible, assign a null value...
something like this:

MyClass instance = new MyClass();

instance.property1 = "A string";
instance.property2 = 21;

then, once i have assigned values, i would like to be able to do
something like this:

foreach (object obj in myinstance)
{
obj = null;
}

What i meant above with object is that of course i may have an int, a
string, etc. so i dont know what type of data i may get and so i want
to traverse the properties despite its type. I dont know if this is
possible, i've read a lot that implementing IEnumerable interfaces it
can be donde but i just cannot find any real example. I also found that
using generics i can handle the value type thing.

Any help would be really appreciated, a URL with an example would be
great :)

Thanks.
You would want to use Reflection to get the properties - not IEnumerable.

Type objType = myinstance.GetType();
PropertyInfo[] properties = objType.GetProperties(); //returns all public
properties
foreach (PropertyInfo property in properties)
{
property.SetValue(myinstance, newValue, null);
}

Where 'newValue' is whatever you want to set the property to. Note, you
showed an example of setting null - but if your property is a type such as
'int', 'double', etc - you will get an exception thrown (since an 'int'
cannot be null).
--
Adam Clauss

Dec 15 '06 #2

P: n/a
visor7,

Reflection is your friend on this. Something you might be interested on is a
very nice example of code on http://blog.guymahieu.com/?p=9. It is a class
to encapsulate all the logic you need. But going with a simple example, it
would look like this:

PropertyInfo[] allProperties = myClass.GetType().GetProperties();
foreach(PropertyInfo thisProperty in allProperties)
{
object value = thisProperty.GetValue(myClass, null);

// to set value
thisProperty.SetValue(myClass, myValue, null);
}

to access the property directly, you could use
myClass.GetType().GetProperty("propertyname). methods you want to use.

Hope it helps,

Robson

"visor7" <gu***********@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
Hi, i've been trying to investigate how to traverse through an object
instance properties or fields with no success. I have read about
IEnumerable interface but that seems to apply only to collection of
objects. What i want to do actually is to traverse through each
property of an instance and if possible, assign a null value...
something like this:

MyClass instance = new MyClass();

instance.property1 = "A string";
instance.property2 = 21;

then, once i have assigned values, i would like to be able to do
something like this:

foreach (object obj in myinstance)
{
obj = null;
}

What i meant above with object is that of course i may have an int, a
string, etc. so i dont know what type of data i may get and so i want
to traverse the properties despite its type. I dont know if this is
possible, i've read a lot that implementing IEnumerable interfaces it
can be donde but i just cannot find any real example. I also found that
using generics i can handle the value type thing.

Any help would be really appreciated, a URL with an example would be
great :)

Thanks.

Dec 15 '06 #3

P: n/a
Thanks for your help, i'll check the URL's you pointed :)

On 15 dic, 17:21, "Robson Siqueira" <rob...@robsonfelix.comwrote:
visor7,

Reflection is your friend on this. Something you might be interested on is a
very nice example of code onhttp://blog.guymahieu.com/?p=9. It is a class
to encapsulate all the logic you need. But going with a simple example, it
would look like this:

PropertyInfo[] allProperties = myClass.GetType().GetProperties();
foreach(PropertyInfo thisProperty in allProperties)
{
object value = thisProperty.GetValue(myClass, null);

// to set value
thisProperty.SetValue(myClass, myValue, null);

}to access the property directly, you could use
myClass.GetType().GetProperty("propertyname). methods you want to use.

Hope it helps,

Robson

"visor7" <gustavo.ru...@gmail.comwrote in messagenews:11*********************@j72g2000cwa.go oglegroups.com...
Hi, i've been trying to investigate how to traverse through an object
instance properties or fields with no success. I have read about
IEnumerable interface but that seems to apply only to collection of
objects. What i want to do actually is to traverse through each
property of an instance and if possible, assign a null value...
something like this:
MyClass instance = new MyClass();
instance.property1 = "A string";
instance.property2 = 21;
then, once i have assigned values, i would like to be able to do
something like this:
foreach (object obj in myinstance)
{
obj = null;
}
What i meant above with object is that of course i may have an int, a
string, etc. so i dont know what type of data i may get and so i want
to traverse the properties despite its type. I dont know if this is
possible, i've read a lot that implementing IEnumerable interfaces it
can be donde but i just cannot find any real example. I also found that
using generics i can handle the value type thing.
Any help would be really appreciated, a URL with an example would be
great :)
Thanks.
Dec 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.