Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Iterating Fields in a class
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Iterating Fields in a class
Divers
Thread ID:
00735219
Message ID:
00735219
Vues:
28
I have some classes that just initialize some fields
Public class MyClass
{
string city ;
string state ;
string etc ; 
}
When I need to read or set the values of the fields, I usually know the field names but now I want to make a generic method that can iterate through all the fields and check for specific content.

Problem is, I can't figure out how to get the values out in a generic way. I have been able to determine that things are fields instead of methods and get the names of the fields but I have yet to get an actual value.

Here is an example of what I am doing:
MyClass oMyClass = new MyClass() 
oMyClass.city = "New Milford";
oMyClass.state = "disillusion" ;
oMyClass.etc = "etcetera,etcetera"; 

System.Reflection.FieldInfo[] oFieldInfo = oMyClass.GetType().GetFields() ;			
for(int i = 0 ; i < oFieldInfo.Length ; i++)
{
MessageBox.Show(oFieldInfo[i].Name.ToString())   ;

}
Anyone know if I’m on the right track here?

Thanks,
Chris
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform