I’m sure I’m not the only one to spend countless hours copying/writing the same code over and over when using System.Configuration.ConfigurationManager. The problems usually start with the fact that ConfigurationManager returns values read from Application Configuration files as strings. Performing explicit casts/parsing is then always left up to you. Everytime you want to access a config value you have to convert values to the correct type.

I wrote an extension method making use of generics. Using this simple method you specify the type of the config value you want to access and if possible it will be returned to the caller as that type.

 using  System;
 using  System.Collections.Generic;
 using  System.Linq;
 using  System.Text;
 using  System.Collections.Specialized;
 
 namespace  SoftwareByDefault.ExtConfig
 {
     public  static  class  ExtConfigManager 
     {
         public  static  T GetValue<T>(this  NameValueCollection  nameValuePairs, string  configKey, T defaultValue) where  T:IConvertible 
         {
             T retValue = default (T);
 
             if  (nameValuePairs.AllKeys.Contains(configKey))
             {
                 string  tmpValue = nameValuePairs[configKey];
 
                 retValue = (T)Convert .ChangeType(tmpValue, typeof (T));
             }
             else 
             {
                 return  defaultValue;
             }
 
             return  retValue;
         }
     }
 }
 

And here is an test application showing how to implement GetValue function:

 using  System;
 using  System.Collections.Generic;
 using  System.ComponentModel;
 using  System.Data;
 using  System.Drawing;
 using  System.Linq;
 using  System.Text;
 using  System.Windows.Forms;
 using  System.Configuration;
 using  SoftwareByDefault.ExtConfig;
 
 namespace  ExtConfigManager.TestApp
 {
     public  partial  class  MainForm  : Form 
     {
         public  MainForm()
         {
             InitializeComponent();
 
             Populate();
         }
 
         private  void  Populate()
         {
             sbyte  sbyteValue = ConfigurationManager .AppSettings.GetValue<sbyte >("SByteValue" , 0);
             LogTypeAndValue<sbyte >(sbyteValue, "SByteValue" );
 
             byte  byteValue = ConfigurationManager .AppSettings.GetValue<byte >("ByteValue" , 0);
             LogTypeAndValue<byte >(byteValue, "ByteValue" );
 
             char  charValue = ConfigurationManager .AppSettings.GetValue<char >("CharValue" , <span' );
             LogTypeAndValue<char >(charValue, "CharValue" );
 
             ushort  ushortValue = ConfigurationManager .AppSettings.GetValue<ushort >("UInt16Value" , 0);
             LogTypeAndValue<ushort >(ushortValue, "UInt16Value" );
 
             short  shortValue = ConfigurationManager .AppSettings.GetValue<short >("Int16Value" , 0);
             LogTypeAndValue<short >(shortValue, "Int16Value" );
 
             uint  uintValue = ConfigurationManager .AppSettings.GetValue<uint >("UInt32Value" , 0);
             LogTypeAndValue<uint >(uintValue, "UInt32Value" );
 
             int  intValue = ConfigurationManager .AppSettings.GetValue<int >("Int32Value" , 0);
             LogTypeAndValue<int >(intValue, "Int32Value" );
 
             ulong  ulongValue = ConfigurationManager .AppSettings.GetValue<ulong >("UInt64Value" , 0);
             LogTypeAndValue<ulong >(ulongValue, "UInt64Value" );
 
             long  longValue = ConfigurationManager .AppSettings.GetValue<long >("Int64Value" , 0);
             LogTypeAndValue<long >(longValue, "Int64Value" );
 
             double  doubleValue = ConfigurationManager .AppSettings.GetValue<double >("DoubleValue" , 0);
             LogTypeAndValue<double >(doubleValue, "DoubleValue" );
 
             decimal  decimalValue = ConfigurationManager .AppSettings.GetValue<decimal >("DecimalValue" , 0);
             LogTypeAndValue<decimal >(decimalValue, "DecimalValue" );
 
             float  floatValue = ConfigurationManager .AppSettings.GetValue<float >("FloatValue" , 0);
             LogTypeAndValue<float >(floatValue, "FloatValue" );
 
             string  stringValue = ConfigurationManager .AppSettings.GetValue<string >("StringValue" , String .Empty);
             LogTypeAndValue<string >(stringValue, "StringValue" );
         }
 
         private  void  LogTypeAndValue<T>(T obj, string  configKey)
         {
             string  description = obj.ToString() + "  ->  "  + obj.GetType().Name + "  ->  "  + configKey;
 
             lstConfigValues.Items.Add(description);
         }
     }
 }
 
 

If you have any questions or comments you can contact me by leaving a comment on this page or contact me via twitter @DefaultSoftware

1 Response to “Extending ConfigurationManager – Get values by type”



  1. 1 Extending ConfigurationManager – Code Sample « Software by Default Trackback on January 12, 2013 at 11:40 AM

Leave a comment




Dewald Esterhuizen

Blog Stats

  • 869,850 hits

Enter your email address to follow and receive notifications of new posts by email.

Join 228 other subscribers

Archives

RSS SoftwareByDefault on MSDN

  • An error has occurred; the feed is probably down. Try again later.