473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to traverse an object properties?

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.proper ty1 = "A string";
instance.proper ty2 = 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
3 7205
"visor7" <gu***********@ gmail.comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.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.proper ty1 = "A string";
instance.proper ty2 = 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.GetT ype();
PropertyInfo[] properties = objType.GetProp erties(); //returns all public
properties
foreach (PropertyInfo property in properties)
{
property.SetVal ue(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
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 ().GetPropertie s();
foreach(Propert yInfo thisProperty in allProperties)
{
object value = thisProperty.Ge tValue(myClass, null);

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

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

Hope it helps,

Robson

"visor7" <gu***********@ gmail.comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.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.proper ty1 = "A string";
instance.proper ty2 = 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
Thanks for your help, i'll check the URL's you pointed :)

On 15 dic, 17:21, "Robson Siqueira" <rob...@robsonf elix.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 ().GetPropertie s();
foreach(Propert yInfo thisProperty in allProperties)
{
object value = thisProperty.Ge tValue(myClass, null);

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

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

Hope it helps,

Robson

"visor7" <gustavo.ru...@ gmail.comwrote in messagenews:11* *************** *****@j72g2000c wa.googlegroups .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.proper ty1 = "A string";
instance.proper ty2 = 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
18067
by: Thieum22 | last post by:
Hi, I try to go through a directory and it's subdirectories to reah the properties of each files. But I have a problem to set active the directory where the files are, in order to display their properties. If anyone know how to improve my method, or know a better one, he's welcome :-) Thanks,
3
3399
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone please help me? TIA. -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
1
5691
by: Raed Sawalha | last post by:
I have a TreeView with 4 levels I need to implement Next and Back capability , my problem in how to traverse the tree to achieve this task.
2
3194
by: john | last post by:
I posted this question to comp.lang.javascript but didn't get a response, so I'll try here. I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the user clicks on a certain button on the page, which is not in the grid, I want to be able to traverse through all the checkboxes in that column and see how many are checked. This is so that I can give them a confirmation dialog before I do an action...
26
5693
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized several parts of the DOM, but this does not include the window object. Thank you
4
3460
by: Rui Maciel | last post by:
I'm trying to go through the elements of a STL list container in such a way that each element can be acessed along with each and every subsequent element from the list. For example, let's say I have the following list: A B C D E F G H... What I am trying to do is something like this: AB, AC, AD, AE, ... , BC, BD, BE, ... , CD, CE, ...
1
2058
by: SonOfGrey | last post by:
Hi! I'm looking for a way to traverse all the controls on a dialog in the order of the tabstop index. Note that I use unmanaged C++, so no GetTabIndex function afaik. The ALMOST perfect function for this looks like: CWnd::GetNextDlgTabItem. BUT...this function skips controls that are disabled (WS_DISABLED),
2
3805
by: rvimalram | last post by:
Hi all, I have a created a multidimesional array,what i need is to traverse that array and return the value of the array in the particular page the array is $one=array("one" =>"Text1"); $two=array( "two" =>array( "a" =>array( "b" =>"Text2"
1
1951
by: Chris Bordeman | last post by:
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 attributes aren't present and would be too slow anyway. My current approach is to recursively traverse a given object using Reflection, storing hash values for strings and value types, then do it again later and compare. Is there a better approach? Maybe someone has...
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.