How to traverse an object properties?

V

visor7

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.
 
A

Adam Clauss

visor7 said:
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).
 
R

Robson Siqueira

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
 
V

visor7

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

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 said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top