Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / FieldFormattingOptions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.DynamicData;
6
7 namespace MonoTests.Common
8 {
9         class FieldFormattingOptions : IFieldFormattingOptions
10         {
11                 Dictionary<string, object> propertyValues = new Dictionary<string, object> ();
12
13                 public bool ApplyFormatInEditMode
14                 {
15                         get { return GetProperty <bool> ("ApplyFormatInEditMode");  }
16                 }
17
18                 public bool ConvertEmptyStringToNull
19                 {
20                         get { return GetProperty <bool> ("ConvertEmptyStringToNull"); }
21                 }
22
23                 public string DataFormatString
24                 {
25                         get { return GetProperty <string> ("DataFormatString"); }
26                 }
27
28                 public bool HtmlEncode
29                 {
30                         get { return GetProperty <bool> ("HtmlEncode"); }
31                 }
32
33                 public string NullDisplayText
34                 {
35                         get { return GetProperty <string> ("NullDisplayText"); }
36                 }
37
38                 T GetProperty<T> (string name)
39                 {
40                         if (String.IsNullOrEmpty (name))
41                                 throw new ArgumentNullException ("name");
42                         
43                         object v;
44                         if (propertyValues.TryGetValue (name, out v)) {
45                                 if (v == null)
46                                         return default (T);
47                                 if (typeof (T).IsAssignableFrom (v.GetType ())) {
48                                         return (T) v;
49                                 }
50
51                                 throw new InvalidOperationException ("Invalid value type. Expected '" + typeof (T) + "' and got '" + v.GetType () + "'");
52                         }
53
54                         return default (T);
55                 }
56
57                 public void SetProperty (string name, object value)
58                 {
59                         if (String.IsNullOrEmpty (name))
60                                 return;
61
62                         if (propertyValues.ContainsKey (name))
63                                 propertyValues[name] = value;
64                         else
65                                 propertyValues.Add (name, value);
66                 }
67         }
68 }