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 7696015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:40:55+00:00 2026-05-31T21:40:55+00:00

I have a class containing several properties (all are strings if it makes any

  • 0

I have a class containing several properties (all are strings if it makes any difference).
I also have a list, which contains many different instances of the class.

While creating some unit tests for my classes I decided I wanted to loop through each object in the list and then loop through each property of that object…

I thought doing this would be as simple as…

foreach (Object obj in theList)
{
     foreach (Property theProperties in obj)
     {
         do some stufff!!;
     }
}

But this didnt work! 🙁
I get this error…

“foreach statement cannot operate on variables of type ‘Application.Object’ because ‘Application.Object’ does not contain a public definition for ‘GetEnumerator'”

Does anyone know of a way of doing this without tons of ifs and loops or without getting into anything too complex?

  • 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-31T21:40:56+00:00Added an answer on May 31, 2026 at 9:40 pm

    Give this a try:

    foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
    {
       // do stuff here
    }
    

    Also please note that Type.GetProperties() has an overload which accepts a set of binding flags so you can filter out properties on a different criteria like accessibility level, see MSDN for more details: Type.GetProperties Method (BindingFlags) Last but not least don’t forget to add the “system.Reflection” assembly reference.

    For instance to resolve all public properties:

    foreach (var propertyInfo in obj.GetType()
                                    .GetProperties(
                                            BindingFlags.Public 
                                            | BindingFlags.Instance))
    {
       // do stuff here
    }
    

    Please let me know whether this works as expected.

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

Sidebar

Related Questions

I have a class library containing several structures each consisting of several value and
I have a Shape class containing potentially many vertices, and I was contemplating making
I have a java class containing all the columns of a database table as
I have a list of items, where each item is a simple class containing
I have a stl::list containing Widget class objects. They need to be sorted according
I have a JSF converter that I use for a SelectItem list containing several
That is, I have a list of class ' Entity ', which is an
I have a class containing a worker thread which receives data from a queue
I have a class which has several fields which are a subclass of another
I have a class containing Linq To SQL objects that are used to populate

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.