How to iterate over numerically named object properties

Posted by Scott Schluer on Stack Overflow See other posts from Stack Overflow or by Scott Schluer
Published on 2011-01-16T14:37:12Z Indexed on 2011/01/16 14:53 UTC
Read the original article Hit count: 148

Filed under:
|
|

So I have a horribly designed class that I can't change that has properties like this:

object.Color1
object.Color2
object.Color3

etc...

How can I iterate through those with a for loop. In other words, something like this:

for (int i = 0; i <= 40; i++)
{
   string PropertyName = "Color" + i;

   if (object.PropertyName != "") 
   { 
       // do something
   }
}

Obviously this code wouldn't work but it gives you an idea of what I'm after. I have to do some processing on each property and I don't want to repeat my code 40 times. :) A loop would be perfect, I'm just not sure how to create the name of the property on the fly.

© Stack Overflow or respective owner

Related posts about c#

Related posts about oop