DataSourceHelper.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.Util
  23. * Class: DataSourceHelper
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>
  28. * Status: 10%
  29. *
  30. * (C) Gaurav Vaish (2001)
  31. */
  32. using System;
  33. using System.Collections;
  34. using System.ComponentModel;
  35. namespace System.Web.Util
  36. {
  37. internal class DataSourceHelper
  38. {
  39. public static IEnumerable GetResolvedDataSource(object source, string member)
  40. {
  41. if(source != null && source is IListSource)
  42. {
  43. IListSource src = (IListSource)source;
  44. IList list = src.GetList();
  45. if(!src.ContainsListCollection)
  46. {
  47. return list;
  48. }
  49. if(list != null && list is ITypedList)
  50. {
  51. ITypedList tlist = (ITypedList)list;
  52. PropertyDescriptorCollection pdc = tlist.GetItemProperties(new PropertyDescriptor[0]);
  53. if(pdc != null && pdc.Count > 0)
  54. {
  55. PropertyDescriptor pd = null;
  56. if(member != null && member.Length > 0)
  57. {
  58. pd = pdc.Find(member, true);
  59. } else
  60. {
  61. pd = pdc[0];
  62. }
  63. if(pd != null)
  64. {
  65. object rv = pd.GetValue(list[0]);
  66. if(rv != null && rv is IEnumerable)
  67. {
  68. return (IEnumerable)rv;
  69. }
  70. }
  71. throw new HttpException(
  72. HttpRuntime.FormatResourceString("ListSource_Missing_DataMember", member));
  73. }
  74. throw new HttpException(
  75. HttpRuntime.FormatResourceString("ListSource_Without_DataMembers"));
  76. }
  77. }
  78. if(source is IEnumerable)
  79. {
  80. return (IEnumerable)source;
  81. }
  82. return null;
  83. }
  84. }
  85. }