FormField.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Web.UI.WebControls;
  8. namespace DymWebForm.Aspx
  9. {
  10. internal static class FormFieldBase
  11. {
  12. public static Func<T, object> GenerateGetterFunc<T>(PropertyInfo pi)
  13. {
  14. //动态构建lambda表达式, 性能应该杠杠的了 乂D
  15. //p=> (object)p.<pi>
  16. var expParam = Expression.Parameter(typeof(T), "p");
  17. var mma = Expression.MakeMemberAccess(
  18. expParam
  19. , pi
  20. );
  21. var mmac = Expression.Convert(mma, typeof(object));
  22. var exp = Expression.Lambda<Func<T, object>>(mmac, expParam);
  23. return exp.Compile();
  24. }
  25. public static Action<T, object> GenerateSetterAction<T>(PropertyInfo pi)
  26. {
  27. //动态构建lambda表达式, 性能应该杠杠的了 乂D
  28. //p=> p.<pi>=(pi.PropertyType)v
  29. var expParamP = Expression.Parameter(typeof(T), "p");
  30. var expParamV = Expression.Parameter(typeof(object), "v");
  31. var expParamVc = Expression.Convert(expParamV, pi.PropertyType);
  32. var mma = Expression.Call(
  33. expParamP
  34. , pi.GetSetMethod()
  35. , expParamVc
  36. );
  37. var exp = Expression.Lambda<Action<T, object>>(mma, expParamP, expParamV);
  38. return exp.Compile();
  39. }
  40. }
  41. internal class FormField<T>
  42. {
  43. //inst
  44. public PropertyInfo PropertyInfo { get; private set; }
  45. //prop
  46. public Func<T, object> GetValue { get; private set; }
  47. public Action<T, object> SetValue { get; private set; }
  48. //feature
  49. public bool IsPK { get; private set; }
  50. public bool IsFK { get; private set; }
  51. public Type FKParentType { get; private set; }
  52. public bool IsFKDisplay { get; private set; }
  53. public int DisplayOrder { get; private set; }
  54. public string FieldName { get; private set; }
  55. public string DisplayName { get; private set; }
  56. public Type DataType { get; private set; }
  57. public bool AllowNull { get; private set; }
  58. public string ControlID { get; private set; }
  59. public bool HasDefaultValueGenerator { get; private set; }
  60. public Func<object> GenerateDefaultValue { get; private set; }
  61. public Type ValidationType { get; private set; }
  62. //ctor
  63. public FormField(PropertyInfo pi)
  64. {
  65. PropertyInfo = pi;
  66. var pia = pi;
  67. if (pi.DeclaringType.IsDefined(typeof (DWFMetaAttribute), true))
  68. {
  69. var mt =
  70. ((DWFMetaAttribute) pi.DeclaringType.GetCustomAttributes(typeof (DWFMetaAttribute), true)[0])
  71. .Type;
  72. var mp = mt.GetProperty(pi.Name);
  73. if (mp != null)
  74. pia = mp;
  75. }
  76. GetValue = FormFieldBase.GenerateGetterFunc<T>(PropertyInfo);
  77. SetValue = FormFieldBase.GenerateSetterAction<T>(PropertyInfo);
  78. DataType = pi.PropertyType;
  79. IsPK = pia.HasAttribute<DWFPKAttribute>();
  80. IsFK = pia.HasAttribute<DWFFKAttribute>();
  81. IsFKDisplay = pia.HasAttribute<DWFFKDisplayAttribute>();
  82. HasDefaultValueGenerator = pia.HasAttribute<DWFDefaultValueGeneratorAttribute>(true);
  83. AllowNull = DataType.IsGenericType && DataType.GetGenericTypeDefinition() == typeof (Nullable<>)
  84. || pia.HasAttribute<DWFAllowNullAttribute>();
  85. ValidationType =
  86. pia.HasAttribute<DWFValidationTypeAttribute>()
  87. ? pia.GetCustomAttributes(typeof (DWFValidationTypeAttribute), true)
  88. .Select(p => ((DWFValidationTypeAttribute) p).ValidationType)
  89. .First()
  90. : null;
  91. if (IsFK)
  92. FKParentType = pia
  93. .GetCustomAttributes(typeof (DWFFKAttribute), true)
  94. .Select(p => ((DWFFKAttribute) p).ParentType)
  95. .FirstOrDefault();
  96. if (HasDefaultValueGenerator)
  97. GenerateDefaultValue =
  98. ((DWFDefaultValueGeneratorAttribute)
  99. pia.GetCustomAttributes(typeof (DWFDefaultValueGeneratorAttribute), true).First())
  100. .GenerateValue;
  101. DisplayOrder = pia.GetCustomAttributes(typeof (DWFDisplayOrderAttribute), true)
  102. .Select(p => (int?) ((DWFDisplayOrderAttribute) p).DisplayOrder)
  103. .FirstOrDefault() ?? int.MaxValue;
  104. FieldName = pi.Name;
  105. DisplayName = pia.GetDescriptionByAttribute(defaultResult: pi.Name);
  106. ControlID = typeof(T).Name + FieldName;
  107. }
  108. //accessor
  109. public WebControl CreateControl()
  110. {
  111. return DymWebFormControlMappnig.GetInst(DataType, ValidationType).CreateControl();
  112. }
  113. public DymWebFormValidation Validation
  114. {
  115. get
  116. {
  117. return DymWebFormControlMappnig.GetInst(DataType, ValidationType).Validation;
  118. }
  119. }
  120. }
  121. internal static class FormFieldFactory<T>
  122. {
  123. private static readonly FormField<T>[] ArrCache;
  124. static FormFieldFactory()
  125. {
  126. var mt = typeof(T)
  127. .GetCustomAttributes(typeof(DWFMetaAttribute), true)
  128. .Select(p => ((DWFMetaAttribute)p).Type)
  129. .FirstOrDefault();
  130. var mp = mt == null
  131. ? null
  132. : mt.GetProperties().ToDictionary(p => p.Name, p => p);
  133. var typ = typeof(T);
  134. ArrCache = typ.GetProperties()
  135. .Where(p =>
  136. //using meta
  137. (mp != null && mp.ContainsKey(p.Name) && mp[p.Name].IsDefined(typeof(DWFIgnoreAttribute), true) == false)
  138. || //not using meta
  139. (mp == null && p.IsDefined(typeof(DWFIgnoreAttribute), true) == false)
  140. )
  141. .SelectArray(p => new FormField<T>(p))
  142. .OrderBy(p => p.DisplayOrder)
  143. .ToArray();
  144. }
  145. public static FormField<T>[] GetFormFields()
  146. {
  147. return ArrCache;
  148. }
  149. public static object GetPKValue(object entity)
  150. {
  151. return GetFormFields()
  152. .Where(p => p.IsPK)
  153. .Select(p => p.GetValue((T)entity))
  154. .First();
  155. }
  156. public static object GetFKValue(object entity)
  157. {
  158. return GetFormFields()
  159. .Where(p => p.IsFKDisplay)
  160. .Select(p => p.GetValue((T)entity))
  161. .First();
  162. }
  163. }
  164. internal static class FormFieldFactoryProxy
  165. {
  166. private static readonly Dictionary<Type, Func<object, object>> ms_dicCachePK;
  167. private static readonly Dictionary<Type, Func<object, object>> ms_dicCacheFK;
  168. static FormFieldFactoryProxy()
  169. {
  170. ms_dicCachePK = new Dictionary<Type, Func<object, object>>();
  171. ms_dicCacheFK = new Dictionary<Type, Func<object, object>>();
  172. }
  173. public static object GetPKValue(object entity)
  174. {
  175. if (entity == null)
  176. throw new ArgumentNullException("entity");
  177. var typ = entity.GetType();
  178. if (ms_dicCachePK.ContainsKey(typ) == false)
  179. {
  180. var typFactory = typeof(FormFieldFactory<>).MakeGenericType(entity.GetType());
  181. var methodName = new Func<object, object>(FormFieldFactory<object>.GetPKValue).Method.Name;
  182. ms_dicCachePK[typ] =
  183. (Func<object, object>)Delegate.CreateDelegate(
  184. typeof(Func<object, object>)
  185. , typFactory.GetMethod(methodName)
  186. );
  187. }
  188. return ms_dicCachePK[typ](entity);
  189. }
  190. public static object GetFKValue(object entity)
  191. {
  192. if (entity == null)
  193. throw new ArgumentNullException("entity");
  194. var typ = entity.GetType();
  195. if (ms_dicCacheFK.ContainsKey(typ) == false)
  196. {
  197. var typFactory = typeof(FormFieldFactory<>).MakeGenericType(entity.GetType());
  198. var metName = new Func<object, object>(FormFieldFactory<object>.GetFKValue).Method.Name;
  199. ms_dicCacheFK[typ] =
  200. (Func<object, object>)Delegate.CreateDelegate(
  201. typeof(Func<object, object>)
  202. , typFactory.GetMethod(metName)
  203. );
  204. }
  205. return ms_dicCacheFK[typ](entity);
  206. }
  207. }
  208. }