• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

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

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
TimPeer
Asked:
TimPeer
  • 3
  • 2
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
Something like this:

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


0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
MisbahCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MisbahCommented:
sorry jaime_olivares , i didn't see your solution until I posted mine.
0
 
Jaime OlivaresSoftware ArchitectCommented:
But mine just iterates in property list
0
 
TimPeerAuthor Commented:
Thanks the quick turnaround
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now