Solved

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

Posted on 2007-11-17
6
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

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

Expert Comment

by:Jaime Olivares
Comment Utility
But mine just iterates in property list
0
 

Author Closing Comment

by:TimPeer
Comment Utility
Thanks the quick turnaround
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now