123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Web.UI.WebControls;
- namespace DymWebForm.Aspx
- {
- internal static class FormFieldBase
- {
- public static Func<T, object> GenerateGetterFunc<T>(PropertyInfo pi)
- {
- //动态构建lambda表达式, 性能应该杠杠的了 乂D
- //p=> (object)p.<pi>
- var expParam = Expression.Parameter(typeof(T), "p");
- var mma = Expression.MakeMemberAccess(
- expParam
- , pi
- );
- var mmac = Expression.Convert(mma, typeof(object));
- var exp = Expression.Lambda<Func<T, object>>(mmac, expParam);
- return exp.Compile();
- }
- public static Action<T, object> GenerateSetterAction<T>(PropertyInfo pi)
- {
- //动态构建lambda表达式, 性能应该杠杠的了 乂D
- //p=> p.<pi>=(pi.PropertyType)v
- var expParamP = Expression.Parameter(typeof(T), "p");
- var expParamV = Expression.Parameter(typeof(object), "v");
- var expParamVc = Expression.Convert(expParamV, pi.PropertyType);
- var mma = Expression.Call(
- expParamP
- , pi.GetSetMethod()
- , expParamVc
- );
- var exp = Expression.Lambda<Action<T, object>>(mma, expParamP, expParamV);
- return exp.Compile();
- }
- }
- internal class FormField<T>
- {
- //inst
- public PropertyInfo PropertyInfo { get; private set; }
- //prop
- public Func<T, object> GetValue { get; private set; }
- public Action<T, object> SetValue { get; private set; }
- //feature
- public bool IsPK { get; private set; }
- public bool IsFK { get; private set; }
- public Type FKParentType { get; private set; }
- public bool IsFKDisplay { get; private set; }
- public int DisplayOrder { get; private set; }
- public string FieldName { get; private set; }
- public string DisplayName { get; private set; }
- public Type DataType { get; private set; }
- public bool AllowNull { get; private set; }
- public string ControlID { get; private set; }
- public bool HasDefaultValueGenerator { get; private set; }
- public Func<object> GenerateDefaultValue { get; private set; }
- public Type ValidationType { get; private set; }
- //ctor
- public FormField(PropertyInfo pi)
- {
- PropertyInfo = pi;
- var pia = pi;
- if (pi.DeclaringType.IsDefined(typeof (DWFMetaAttribute), true))
- {
- var mt =
- ((DWFMetaAttribute) pi.DeclaringType.GetCustomAttributes(typeof (DWFMetaAttribute), true)[0])
- .Type;
- var mp = mt.GetProperty(pi.Name);
- if (mp != null)
- pia = mp;
- }
- GetValue = FormFieldBase.GenerateGetterFunc<T>(PropertyInfo);
- SetValue = FormFieldBase.GenerateSetterAction<T>(PropertyInfo);
- DataType = pi.PropertyType;
- IsPK = pia.HasAttribute<DWFPKAttribute>();
- IsFK = pia.HasAttribute<DWFFKAttribute>();
- IsFKDisplay = pia.HasAttribute<DWFFKDisplayAttribute>();
- HasDefaultValueGenerator = pia.HasAttribute<DWFDefaultValueGeneratorAttribute>(true);
- AllowNull = DataType.IsGenericType && DataType.GetGenericTypeDefinition() == typeof (Nullable<>)
- || pia.HasAttribute<DWFAllowNullAttribute>();
- ValidationType =
- pia.HasAttribute<DWFValidationTypeAttribute>()
- ? pia.GetCustomAttributes(typeof (DWFValidationTypeAttribute), true)
- .Select(p => ((DWFValidationTypeAttribute) p).ValidationType)
- .First()
- : null;
- if (IsFK)
- FKParentType = pia
- .GetCustomAttributes(typeof (DWFFKAttribute), true)
- .Select(p => ((DWFFKAttribute) p).ParentType)
- .FirstOrDefault();
- if (HasDefaultValueGenerator)
- GenerateDefaultValue =
- ((DWFDefaultValueGeneratorAttribute)
- pia.GetCustomAttributes(typeof (DWFDefaultValueGeneratorAttribute), true).First())
- .GenerateValue;
- DisplayOrder = pia.GetCustomAttributes(typeof (DWFDisplayOrderAttribute), true)
- .Select(p => (int?) ((DWFDisplayOrderAttribute) p).DisplayOrder)
- .FirstOrDefault() ?? int.MaxValue;
- FieldName = pi.Name;
- DisplayName = pia.GetDescriptionByAttribute(defaultResult: pi.Name);
- ControlID = typeof(T).Name + FieldName;
- }
- //accessor
- public WebControl CreateControl()
- {
- return DymWebFormControlMappnig.GetInst(DataType, ValidationType).CreateControl();
- }
- public DymWebFormValidation Validation
- {
- get
- {
- return DymWebFormControlMappnig.GetInst(DataType, ValidationType).Validation;
- }
- }
- }
- internal static class FormFieldFactory<T>
- {
- private static readonly FormField<T>[] ArrCache;
- static FormFieldFactory()
- {
- var mt = typeof(T)
- .GetCustomAttributes(typeof(DWFMetaAttribute), true)
- .Select(p => ((DWFMetaAttribute)p).Type)
- .FirstOrDefault();
- var mp = mt == null
- ? null
- : mt.GetProperties().ToDictionary(p => p.Name, p => p);
- var typ = typeof(T);
- ArrCache = typ.GetProperties()
- .Where(p =>
- //using meta
- (mp != null && mp.ContainsKey(p.Name) && mp[p.Name].IsDefined(typeof(DWFIgnoreAttribute), true) == false)
- || //not using meta
- (mp == null && p.IsDefined(typeof(DWFIgnoreAttribute), true) == false)
- )
- .SelectArray(p => new FormField<T>(p))
- .OrderBy(p => p.DisplayOrder)
- .ToArray();
- }
- public static FormField<T>[] GetFormFields()
- {
- return ArrCache;
- }
- public static object GetPKValue(object entity)
- {
- return GetFormFields()
- .Where(p => p.IsPK)
- .Select(p => p.GetValue((T)entity))
- .First();
- }
- public static object GetFKValue(object entity)
- {
- return GetFormFields()
- .Where(p => p.IsFKDisplay)
- .Select(p => p.GetValue((T)entity))
- .First();
- }
- }
- internal static class FormFieldFactoryProxy
- {
- private static readonly Dictionary<Type, Func<object, object>> ms_dicCachePK;
- private static readonly Dictionary<Type, Func<object, object>> ms_dicCacheFK;
- static FormFieldFactoryProxy()
- {
- ms_dicCachePK = new Dictionary<Type, Func<object, object>>();
- ms_dicCacheFK = new Dictionary<Type, Func<object, object>>();
- }
- public static object GetPKValue(object entity)
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- var typ = entity.GetType();
- if (ms_dicCachePK.ContainsKey(typ) == false)
- {
- var typFactory = typeof(FormFieldFactory<>).MakeGenericType(entity.GetType());
- var methodName = new Func<object, object>(FormFieldFactory<object>.GetPKValue).Method.Name;
- ms_dicCachePK[typ] =
- (Func<object, object>)Delegate.CreateDelegate(
- typeof(Func<object, object>)
- , typFactory.GetMethod(methodName)
- );
- }
- return ms_dicCachePK[typ](entity);
- }
- public static object GetFKValue(object entity)
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- var typ = entity.GetType();
- if (ms_dicCacheFK.ContainsKey(typ) == false)
- {
- var typFactory = typeof(FormFieldFactory<>).MakeGenericType(entity.GetType());
- var metName = new Func<object, object>(FormFieldFactory<object>.GetFKValue).Method.Name;
- ms_dicCacheFK[typ] =
- (Func<object, object>)Delegate.CreateDelegate(
- typeof(Func<object, object>)
- , typFactory.GetMethod(metName)
- );
- }
- return ms_dicCacheFK[typ](entity);
- }
- }
- }
|