CustomerMetadataProvider.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Xml.Linq;
  8. using Bowin.Common.XML;
  9. using System.Reflection;
  10. namespace EMIS.Web.Controls.DataAnnotations
  11. {
  12. public class CustomerMetadataProvider : DataAnnotationsModelMetadataProvider
  13. {
  14. private class SpecialTypeConfig
  15. {
  16. public string FullName { get; set; }
  17. public string MetadataType { get; set; }
  18. public string Assembly { get; set; }
  19. }
  20. private List<SpecialTypeConfig> specialTypeList;
  21. private List<SpecialTypeConfig> SpecialTypeList
  22. {
  23. get
  24. {
  25. if (specialTypeList == null)
  26. {
  27. string configPath = HttpContext.Current.Server.MapPath("~/Config/DataAnnotations.xml");
  28. XElement config = XElement.Load(new System.IO.FileStream(configPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read), LoadOptions.None);
  29. if (config.Elements().Count() == 0)
  30. {
  31. specialTypeList = new List<SpecialTypeConfig>();
  32. }
  33. specialTypeList = (from t in config.Elements("MetadataTypes")
  34. from x in t.Elements("Metadata")
  35. select new SpecialTypeConfig
  36. {
  37. FullName = x.Attribute("Name").GetStringValue(),
  38. MetadataType = x.Attribute("MetadataType").GetStringValue(),
  39. Assembly = t.Attribute("Assembly").GetStringValue()
  40. }).ToList();
  41. }
  42. return specialTypeList;
  43. }
  44. }
  45. protected override System.ComponentModel.ICustomTypeDescriptor GetTypeDescriptor(Type type)
  46. {
  47. string typeKeyName = type.Namespace + "." + type.Name;
  48. var specialType = this.SpecialTypeList.FirstOrDefault(x => x.FullName == typeKeyName);
  49. if (specialType != null)
  50. {
  51. var metaDataAssambly = Assembly.Load(specialType.Assembly);
  52. var metaDatType = metaDataAssambly.GetType(specialType.MetadataType);
  53. if (metaDatType != null)
  54. {
  55. return (new AssociatedMetadataTypeTypeDescriptionProvider(type, metaDatType))
  56. .GetTypeDescriptor(type);
  57. }
  58. }
  59. return base.GetTypeDescriptor(type);
  60. }
  61. }
  62. }