mercredi 15 avril 2015

dynamically populate properties of a class

I have the following code which takes some values from my App.config and tries to use them to populate the properties of a class.



foreach (string ReferenceKey in Utilities.CSVToList(ConfigurationManager.AppSettings[source + ":Keys"]))
{
if (ConfigurationManager.AppSettings[ReferenceKey] != null && Incoming_Values.ContainsKey(ConfigurationManager.AppSettings[ReferenceKey]))
{
PropertyInfo info = MyCustomClass.GetType().GetProperty(ReferenceKey.Split(':')[1]);
info.SetValue(MyCustomClass, Incoming_Values[ConfigurationManager.AppSettings[ReferenceKey]]);
}
else
{
return null;
}
}


The problem I'm having is that obviously the KVP I get from the config file are all going to be of type string, but the properties of the class are strongly typed. I'm trying to "loosely couple" the values and the class, but I have an issue where the property is not a string (e.g.. it's a datetime or an int or even a class of my own).


Does anyone have any idea how I would handle such a thing? Should I build a tranlator class or something?






Aucun commentaire:

Enregistrer un commentaire