Solved

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

Posted on 2007-11-17
6
270 Views
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?
0
Comment
Question by:TimPeer
  • 3
  • 2
6 Comments
 
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
}


0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 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);
        System.Windows.Forms.MessageBox.Show(text);
}

Open in new window

0
 
LVL 9

Accepted Solution

by:
Misbah earned 300 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
    {
        [STAThread]
        static void Main(string[] args)
        {
            Type TypeToReflect = Type.GetType("System.Int32");
            System.Reflection.MemberInfo[] Members =type.GetMembers();

            Console.WriteLine ("Members of "+TypeToReflect.ToString ());
            Console.WriteLine();
            foreach (MemberInfo member in Members )
                Console.WriteLine(member);
            Console.ReadLine ();
        }
    }
}
http://www.codeproject.com/csharp/IntroReflection.asp

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

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

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

Expert Comment

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

Author Closing Comment

by:TimPeer
ID: 31409757
Thanks the quick turnaround
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

786 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