[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


How can I create an iterator exposing each property of a record or structure.

Posted on 2007-11-17
Medium Priority
Last Modified: 2010-04-21
Is it possible to iterate through properties in a class object. Classes contain an unknown number of properties. I wish to iterate through each property a run-time. How do I implement an iterator for a class?
Question by:TimPeer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306005
Something like this:

foreach (System.Reflection.PropertyInfo p in yourClass.GetType().GetProperties())
      // do something with p

LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 800 total points
ID: 20306023
if you have some specifc object (instance) you can print all its properties with:

yourClass yourObj = ...;  // assign here
foreach (System.Reflection.PropertyInfo p in yourClass.GetType().GetProperties())
        object prop = p.GetValue(yourObj);
        string text = string.Format("Value of property {0} is {1}", p, prop);

Open in new window


Accepted Solution

Misbah earned 1200 total points
ID: 20306031
You need Reflection to do this
search for reflection and you will find many examples.

here is one
using System;
using System.Reflection ;

namespace ReflectionDemoCSharp
    class ReflectedTypes
        static void Main(string[] args)
            Type TypeToReflect = Type.GetType("System.Int32");
            System.Reflection.MemberInfo[] Members =type.GetMembers();

            Console.WriteLine ("Members of "+TypeToReflect.ToString ());
            foreach (MemberInfo member in Members )
            Console.ReadLine ();

you can reuse the above code which extract the methods of a type/class
and you can add to it this method to get all properties of a type/class
type.GetProperties() .... this will return all properties of the class

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 20306038
sorry jaime_olivares , i didn't see your solution until I posted mine.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20306051
But mine just iterates in property list

Author Closing Comment

ID: 31409757
Thanks the quick turnaround

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question