0

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
stumped221
  • 89
  • 4
  • 18

2 Answers2

1

There are existing utilities in the framework that can help you with that.

One simple function is the Convert.ChangeType method, but it's not very customizable and is only limited to conversions between types that are part of what IConvertible can manage. If that's sufficient, go ahead.

Here's another approach that's more pluggable: you can use TypeConverters. You can get a TypeConverter from the TypeDescriptor class:

TypeDescriptor.GetConverter(targetType).ConvertFromInvariantString(configValue)

You can define custom TypeConverter classes if needed. This may or may not be better than implementing your own solution from scratch. It's up to you to decide depending on your needs.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
1

Take a look at custom configuration sections, they provide you with strongly typed access to the app.config values. You can then loose couple this to your classes as you wish.

How to: Create Custom Configuration Sections Using ConfigurationSection (MSDN)

How to create custom config section in app.config (stack overflow)

Community
  • 1
  • 1
Michael Sander
  • 2,677
  • 23
  • 29
  • I don't think this is going to work actually.. the value is coming from an array passed in via SOAP message (which is not strongly typed by design), just an array of strings. The only thing I'm using the config file for is to find pointers to the values in the array. I'm sorry, I probably should have left more detail in my question. "Incoming_Values[ConfigurationManager.AppSettings[ReferenceKey]]" – stumped221 Apr 15 '15 at 20:43
  • @stumped221 Talking about loose coupling, I think you should revise your design. Load settings in one place, use them in some other, without caring where and how they are stored. – Michael Sander Apr 15 '15 at 20:50
  • I completely agree with you.. this is a learning experience for me, so I'm on the journey to figure out how to "revise my design" or even define and grasp a good design that is loosely coupled. – stumped221 Apr 15 '15 at 20:55