Removed Consoles and ^Ms
[mono.git] / mcs / class / System / System.Configuration / AppSettingsReader.cs
1 //
2 // System.Configuration.AppSettingsReader
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9
10 using System.Reflection;
11 using System.Collections.Specialized;
12
13 namespace System.Configuration
14 {
15         public class AppSettingsReader
16         {
17                 NameValueCollection appSettings;
18
19                 public AppSettingsReader ()
20                 {
21                         appSettings = ConfigurationSettings.AppSettings;
22                 }
23
24                 public object GetValue (string key, Type type)
25                 {
26                         if (key == null)
27                                 throw new ArgumentNullException ("key");
28
29                         if (type == null)
30                                 throw new ArgumentNullException ("type");
31
32                         string value = appSettings [key];
33                         if (value == null)
34                                 throw new InvalidOperationException ("'" + key + "' could not be found.");
35
36                         if (type == typeof (string))
37                                 return value;
38                         
39                         MethodInfo parse = type.GetMethod ("Parse", new Type [] {typeof (string)});
40                         if (parse == null)
41                                 throw new InvalidOperationException ("Type " + type + " does not have a Parse method");
42
43                         object result = null;
44                         try {
45                                 result = parse.Invoke (null, new object [] {value});
46                         } catch (Exception e) {
47                                 throw new InvalidOperationException ("Parse error.", e);
48                         }
49
50                         return result;
51                 }
52         }
53 }
54