keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.csharp

 #1  
12-15-06, 08:57 PM
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.
 #2  
12-15-06, 09:12 PM
Adam Clauss
"visor7" <gustavo.rubio> wrote in message
news:7760
[..]
> 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).
 #3  
12-15-06, 09:21 PM
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

"visor7" <gustavo.rubio> wrote in message
news:7760
[..]
 #4  
12-16-06, 08:58 AM
visor7
Thanks for your help, i'll check the URL's you pointed :)

On 15 dic, 17:21, "Robson Siqueira" <rob> wrote:
[..]
Similar Threads
Array within class object how do I traverse?

Hi I frequently find that that I move std::vector member or array members outside of a class object simply because of the difficulty of providing an interface to the member...

continuous - want to change properties of object based on another object data

Having trouble with this one continuous form......i've tried many alternatives on the load and open event without luck. The fields/record are Category , Subcategory , Item ,...

Need to traverse an object structure using Reflection

Hi all. I need to generically detect changes in any object from one point to the next. I can't modify the objects, nor can I use traditional serialization because the...

How can I traverse a list of properties of a class ?

Hi, I have a class that has defined like 30 string constants. I need to test a string against each one of this contants to see if it matches any of them. I was thiking to...


All times are GMT. The time now is 02:19 AM. | Privacy Policy