using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.ComponentModel.DataAnnotations; using System.Xml.Linq; using Bowin.Common.XML; using System.Reflection; namespace EMIS.Web.Controls.DataAnnotations { public class CustomerMetadataProvider : DataAnnotationsModelMetadataProvider { private class SpecialTypeConfig { public string FullName { get; set; } public string MetadataType { get; set; } public string Assembly { get; set; } } private List specialTypeList; private List SpecialTypeList { get { if (specialTypeList == null) { string configPath = HttpContext.Current.Server.MapPath("~/Config/DataAnnotations.xml"); XElement config = XElement.Load(new System.IO.FileStream(configPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read), LoadOptions.None); if (config.Elements().Count() == 0) { specialTypeList = new List(); } specialTypeList = (from t in config.Elements("MetadataTypes") from x in t.Elements("Metadata") select new SpecialTypeConfig { FullName = x.Attribute("Name").GetStringValue(), MetadataType = x.Attribute("MetadataType").GetStringValue(), Assembly = t.Attribute("Assembly").GetStringValue() }).ToList(); } return specialTypeList; } } protected override System.ComponentModel.ICustomTypeDescriptor GetTypeDescriptor(Type type) { string typeKeyName = type.Namespace + "." + type.Name; var specialType = this.SpecialTypeList.FirstOrDefault(x => x.FullName == typeKeyName); if (specialType != null) { var metaDataAssambly = Assembly.Load(specialType.Assembly); var metaDatType = metaDataAssambly.GetType(specialType.MetadataType); if (metaDatType != null) { return (new AssociatedMetadataTypeTypeDescriptionProvider(type, metaDatType)) .GetTypeDescriptor(type); } } return base.GetTypeDescriptor(type); } } }