FieldFormattingOptions.cs 1.5 KB

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