The Labs \ Source Viewer \ SSCLI \ System.Configuration \ AppSettingsReader

  1. //------------------------------------------------------------------------------
  2. // <copyright file="AppSettingsReader.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Reflection;
  17. using System.Configuration;
  18. using System.Collections.Specialized;
  19. using System.Globalization;
  20. namespace System.Configuration
  21. {
  22.    
  23.     /// <devdoc>
  24.     /// The AppSettingsReader class provides a wrapper for System.Configuration.ConfigurationManager.AppSettings
  25.     /// which provides a single method for reading values from the config file of a particular type.
  26.     /// </devdoc>
  27.     public class AppSettingsReader
  28.     {
  29.         private NameValueCollection map;
  30.         static Type stringType = typeof(string);
  31.         static Type[] paramsArray = new Type[] {stringType};
  32.         static string NullString = "None";
  33.        
  34.         /// <devdoc>
  35.         /// Constructor
  36.         /// </devdoc>
  37.         public AppSettingsReader()
  38.         {
  39.             map = System.Configuration.ConfigurationManager.AppSettings;
  40.         }
  41.        
  42.         /// <devdoc>
  43.         /// Gets the value for specified key from ConfigurationManager.AppSettings, and returns
  44.         /// an object of the specified type containing the value from the config file. If the key
  45.         /// isn't in the config file, or if it is not a valid value for the given type, it will
  46.         /// throw an exception with a descriptive message so the user can make the appropriate
  47.         /// change
  48.         /// </devdoc>
  49.         public object GetValue(string key, Type type)
  50.         {
  51.             if (key == null)
  52.                 throw new ArgumentNullException("key");
  53.             if (type == null)
  54.                 throw new ArgumentNullException("type");
  55.            
  56.             string val = map[key];
  57.            
  58.             if (val == null)
  59.                 throw new InvalidOperationException(SR.GetString(SR.AppSettingsReaderNoKey, key));
  60.            
  61.             if (type == stringType) {
  62.                 // It's a string, so we can ALMOST just return the value. The only
  63.                 // tricky point is that if it's the string "(None)", then we want to
  64.                 // return null. And of course we need a way to represent the string
  65.                 // (None), so we use ((None)), and so on... so it's a little complicated.
  66.                 int NoneNesting = GetNoneNesting(val);
  67.                 if (NoneNesting == 0) {
  68.                     // val is not of the form ((..((None))..))
  69.                     return val;
  70.                 }
  71.                 else if (NoneNesting == 1) {
  72.                     // val is (None)
  73.                     return null;
  74.                 }
  75.                 else {
  76.                     // val is of the form ((..((None))..))
  77.                     return val.Substring(1, val.Length - 2);
  78.                 }
  79.             }
  80.             else {
  81.                 try {
  82.                     return Convert.ChangeType(val, type, CultureInfo.InvariantCulture);
  83.                 }
  84.                 catch (Exception) {
  85.                     string displayString = (val.Length == 0) ? SR.AppSettingsReaderEmptyString : val;
  86.                     throw new InvalidOperationException(SR.GetString(SR.AppSettingsReaderCantParse, displayString, key, type.ToString()));
  87.                 }
  88.             }
  89.         }
  90.        
  91.         private int GetNoneNesting(string val)
  92.         {
  93.             int count = 0;
  94.             int len = val.Length;
  95.             if (len > 1) {
  96.                 while (val[count] == '(' && val[len - count - 1] == ')') {
  97.                     count++;
  98.                 }
  99.                 if (count > 0 && string.Compare(NullString, 0, val, count, len - 2 * count, StringComparison.Ordinal) != 0) {
  100.                     // the stuff between the parens is not "None"
  101.                     count = 0;
  102.                 }
  103.             }
  104.             return count;
  105.         }
  106.     }
  107. }

Developer Fusion