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

  • Home
  • SEARCH
  • 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 this horribly stripped delphi code that basically login to server, save cookie
have written this little class, which generates a UUID every time an object of
I have a JPanel that looks something like this: JPanel panel = new JPanel();
I have some code that has a dynamic-class system in C++ that has a
I have a table, horribly designed (not my doing thankfully), that stores data in
I have a horribly formated, tab delimited, CSV that I'm trying to clean up.
I have a DAG representing a list of properties. These properties are such that
I have the following SQL query that performs horribly due to the select count(1)
I have a large ability file that decides what exactly users can do by
I have this query that is bothering me; it is encapsulated as a new

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.