1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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<SpecialTypeConfig> specialTypeList;
- private List<SpecialTypeConfig> 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<SpecialTypeConfig>();
- }
- 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);
- }
- }
- }
|