DataBinder.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // System.Web.UI.DataBinder.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.Reflection;
  12. namespace System.Web.UI {
  13. public sealed class DataBinder
  14. {
  15. public DataBinder ()
  16. {
  17. }
  18. public static object Eval (object container, string expression)
  19. {
  20. return GetPropertyValue (container, expression);
  21. }
  22. public static string Eval (object container, string expression, string format)
  23. {
  24. return GetPropertyValue (container, expression, format);
  25. }
  26. [MonoTODO]
  27. public static object GetIndexedPropertyValue (object container, string expr)
  28. {
  29. throw new NotImplementedException ();
  30. }
  31. [MonoTODO]
  32. public static string GetIndexedPropertyValue (object container, string expr, string format)
  33. {
  34. throw new NotImplementedException ();
  35. }
  36. public static object GetPropertyValue (object container, string propName)
  37. {
  38. if (container == null || propName == null)
  39. throw new ArgumentException ();
  40. Type type = container.GetType ();
  41. PropertyInfo prop = type.GetProperty (propName);
  42. if (prop == null)
  43. throw new HttpException ("Property " + propName + " not found in " +
  44. type.ToString ());
  45. MethodInfo getm = prop.GetGetMethod ();
  46. if (getm == null)
  47. throw new HttpException ("Cannot find get accessor for " + propName +
  48. " in " + type.ToString ());
  49. return getm.Invoke (container, null);
  50. }
  51. public static string GetPropertyValue (object container, string propName, string format)
  52. {
  53. object result;
  54. result = GetPropertyValue (container, propName);
  55. if (result == null)
  56. return String.Empty;
  57. if (format == null)
  58. return result.ToString ();
  59. return String.Format (format, result);
  60. }
  61. }
  62. }