How can I "Enumerate" a class (Or find its member properties)

G

GregoryJ

I have the following Class:

Public class Contact()
{
private int contactID;
private string contactName;
private datetime dateOfBirth;

public int ContactID
{
get { return contactID; }
set { contactID = return; }
}
public string ContactName
{
get { return contactName; }
set { contactName = return; }
}
public DateTime DateOfBirth
{
get { return dateOfBirth; }
set { dateOfBirth = return; }
}
}

This class contains three properties, ContactID, ContactName and
DateOfBirth. I'd like to examine the class without knowing anything about it,
other than the class' name. In other words, if I have instatiated the class,
is it possible to step throught the class properties via a loop without
knowing the property names?

Contact myContact = new Contact();

I'd like to get the name of the properties (ContactID, ContactName,
DateOfBirth), the data type of each property and the value of each property
when its initialized.

I don't want to refer to the ContactName as myContact.ContactName. I'm
looking to step though this class via a loop and get the "ContactName"
property and values.

Any help on achieving this would be appreciated.

Thanks.
 
G

GregoryJ

Great. This does exactly what I was looking for. Plus, now I understand more
what "Reflection" is about. Thanks.

I've got just two more questions.

1. I did the following to get the Properties from my class.

private void EnumCit()
{
Contact contact = new Contact();
PropertyInfo[] myProperties = contact.GetType().GetProperties();
contact.ContactID = 1;
contact.Load();
int i = 0;

do
{
string strName = myProperties.Name.ToString();
string strType = myProperties.PropertyType.ToString();
this.txtEnumClasses.Text +=
strName + " - " +
strType + " - Value = ";
i++;
} while (i < myProperties.Length);
}

This lists each Property Name (Field Names, ContactID, ContactName, etc.),
plus their Types (i.e. String, Int32, etc.). But, I'm not having any luck
with getting the value for each property. In the same code above, I am
running my contact.Load Method which loads a contact, so I do have one
instantiated. I tried the following:

string strValue = myProperties.GetValue(strName, null).ToString();

This gives me an error stating: Object does not match Target Type. I tried
changing the strValue and strName to Object types and that gave the same
error. Can you tell me how I should go about retrieving the value of each
Property? (i.e. ContactName would equal "Joe Williams").

2. In addition to Enumerating all of the properties of a Class Module, I'd
also like to do that same with all Web Controls on a Web-Page, including
"child" controls within "parent" controls. I'd like to pass my web page (i.e.
System.Web.UI.Page) to a method to get a dataset back containing all of the
controls, along with their types (i.e. System.Web.UI.WebControls.TextBox,
System.Web.UI.WebControls.Label, etc). Plus, I'd like to get the values in
each control. Would you be able to help me with doing this?

Thanks for your help in advance.

Greg
 
B

Ben Voigt [C++ MVP]

GregoryJ said:
Great. This does exactly what I was looking for. Plus, now I
understand more what "Reflection" is about. Thanks.

I've got just two more questions.

1. I did the following to get the Properties from my class.

private void EnumCit()
{
Contact contact = new Contact();
PropertyInfo[] myProperties = contact.GetType().GetProperties();
contact.ContactID = 1;
contact.Load();
int i = 0;

do
{
string strName = myProperties.Name.ToString();
string strType = myProperties.PropertyType.ToString();
this.txtEnumClasses.Text +=
strName + " - " +
strType + " - Value = ";
i++;
} while (i < myProperties.Length);
}

This lists each Property Name (Field Names, ContactID, ContactName,
etc.), plus their Types (i.e. String, Int32, etc.). But, I'm not
having any luck with getting the value for each property. In the same
code above, I am running my contact.Load Method which loads a
contact, so I do have one instantiated. I tried the following:

string strValue = myProperties.GetValue(strName, null).ToString();


no need to pass strName, *which* property is designated by myProperties

Instead you need to pass the object to read the property from, contact.

The PropertyInfo objects are associated with the Type (Contact), not the
instance (contact), so you need to supply the instance to GetValue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top