Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3756976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:56:05+00:00 2026-05-19T09:56:05+00:00

So I have a horribly designed class that I can’t change that has properties

  • 0

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.

EDIT: Ok so I’ve tried the following code:

for (int i = 1; i <= 20; i++ )
            {
                var type = pendingProduct.GetType();

                var colorProperty = type.GetProperty("Color" + i);
                string colorValue = colorProperty.GetValue(type, null).ToString();

                var colorSkuProperty = type.GetProperty("Color" + i + "SKU");
                string colorSkuValue = colorSkuProperty.GetValue(type, null).ToString();

                if (String.IsNullOrEmpty(colorValue)) continue;

                ProductColor color = new ProductColor {Color = colorValue, ProductSizes = productSizes};
                if (!String.IsNullOrEmpty(colorSkuValue)) color.SKU = colorSkuValue;
            }

I’m getting an error “Object does not match target type” on this line:

string colorValue = colorProperty.GetValue(type, null).ToString();

Am I doing something wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:56:06+00:00Added an answer on May 19, 2026 at 9:56 am

    You’re looking for reflection:

    PropertyInfo property = typeof(SomeType).GetProperty("Color" + i);
    string value = (string)property.GetValue(obj, null);
    

    Note that this will be slow.
    If you do it many times, you can make it faster by caching Delegate.CreateDelegate(..., property.GetGetMethod()) in an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling I'm doing this horribly, horribly wrong. Nested for loops? What
I have this one gigantic complex LINQ to SQL query that I need to
I want that list, because if something horrible happens, and I'll have to reinstall
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have two database tables, subscription and transaction , where one subscription can have
I have acquired the task of updating some horribly outdated JavaScript code which I
I have a view that I have been trying to map in either entity
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.