0

I've seen a couple of threads on how to loop through properties of a specified class, but I need to know how to loop through all custom classes for a given ASPX.

Writing out property-name:property-value of every possible class/property is the desired result.

I should note that the custom classes I want to iterate through, are members of a page that uses a master page. Don't know if that makes a difference.

Is this possible?

s15199d
  • 7,261
  • 11
  • 43
  • 70
  • Custom classes as in the class for a custom control in the ASPX, or what? I don't understand what you are trying to do... – Brian Mains Nov 28 '11 at 19:15
  • Yes, I'm particularly interested in the values of custom classes that populate the values of custom controls on the page. – s15199d Nov 28 '11 at 19:16

3 Answers3

0

I have accomplished something similar by using Reflection, however I don't think you can actually loop through all of your classes without indicating at least their names.

Code:

using System; 
using System.Reflection; 

    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Age = 27;
            person.Name = "Fernando Vezzali";

            Type type = typeof(Person);
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                Console.WriteLine("{0} = {1}", property.Name, property.GetValue(person, null));
            }

            Console.Read();
        }
    }

Person Class:

class Person
{
    private int age;
    private string name;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

Reference: http://wiki.asp.net/page.aspx/474/how-to-iterate-through-all-properties-of-a-class/

Good luck!

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
  • Right. This is what I referred to as a "specified class" in my question. I need to loop through all classes for the page dynamically, if that's possible. – s15199d Nov 28 '11 at 19:20
  • Check this out, it might help you http://stackoverflow.com/questions/949246/how-to-get-all-classes-within-namespace – Hanlet Escaño Nov 28 '11 at 19:25
0

check this code in your aspx:

foreach (var item in Assembly.GetEntryAssembly().GetExportedTypes())
{
    var obj = item.Assembly.CreateInstance(item.FullName);
    foreach (var prop in item.GetProperties())
    {
        Console.WriteLine(item.Name + "," + prop.Name + "," + prop.GetValue(obj, null));
    }
}
Reza ArabQaeni
  • 4,848
  • 27
  • 46
0

This may not be what you're looking for, but you could serialize the classes and just spit the XML out onto the page. I don't know what your intentions are, but this might be a more uniformed approach.

Here's an article to get you started:
http://www.codeproject.com/KB/XML/xml_serializationasp.aspx

James Johnson
  • 45,496
  • 8
  • 73
  • 110