123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Web.UI.WebControls;
- using DymWebForm.Common.Attributes;
- using DymWebForm.Common.Linq;
- using DymWebForm.Common.Reflection;
- namespace DymWebForm
- {
- /// <summary>
- /// Description of MyClass.
- /// </summary>
- public class FormField
- {
- //inst
- public PropertyInfo PropertyInfo { get; private set; }
- //prop
- public Func<object, object> GetValue { get; private set; }
- public Action<object, 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 = PropertyInfo.GenerateGetterFunc();
- SetValue = PropertyInfo.GenerateSetterAction();
- 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 = pi.DeclaringType.Name + FieldName;
-
- }
- public WebControl CreateControl()
- {
- return DymWebFormControlMappnig.GetInst(DataType, ValidationType).CreateControl();
- }
- public DymWebFormValidation Validation
- {
- get
- {
- return DymWebFormControlMappnig.GetInst(DataType, ValidationType).Validation;
- }
- }
- }
- internal class FormFieldFactory
- {
- private readonly FormField[] ArrCache;
- public FormFieldFactory(Type t)
- {
- var mt = t
- .GetCustomAttributes(typeof(DWFMetaAttribute), true)
- .Select(p => ((DWFMetaAttribute)p).Type)
- .FirstOrDefault();
- var mp = mt == null
- ? null
- : mt.GetProperties().ToDictionary(p => p.Name, p => p);
- ArrCache = t.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(p))
- .OrderBy(p => p.DisplayOrder)
- .ToArray();
- }
- public FormField[] GetFormFields()
- {
- return ArrCache;
- }
- public object GetPKValue(object entity)
- {
- return GetFormFields()
- .Where(p => p.IsPK)
- .Select(p => p.GetValue(entity))
- .First();
- }
- public object GetFKValue(object entity)
- {
- return GetFormFields()
- .Where(p => p.IsFKDisplay)
- .Select(p => p.GetValue(entity))
- .First();
- }
- }
- }
|