“单例”的属性访问器
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Linq.Expressions; namespace BaseModel { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class SelectAttribute : Attribute { private EnumFormularType _enumformulartype; private string _acronym; private string _column; public SelectAttribute(EnumFormularType enumformulartype, string acronym = null, string column = null) { _enumformulartype = enumformulartype; _acronym = acronym; _column = column; } /// <summary> /// 运算方式 /// </summary> public EnumFormularType EnumFormularType { get { return _enumformulartype; } } /// <summary> /// 表别名 /// </summary> public string Acronym { get { return _acronym; } } /// <summary> /// 表列名 /// </summary> public string Column { get { return _column; } } } [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class AliasAttribute : Attribute { private string _aliasName; private bool _isSequence; /// <summary> /// /// </summary> /// <param name="aliasName">别名</param> /// <param name="isAlias"></param> public AliasAttribute(string aliasName, bool isSequence = false) { _aliasName = aliasName; _isSequence = isSequence; } #region Public Properties /// <summary> /// 别名 /// </summary> public string AliasName { get { return _aliasName; } } /// <summary> /// 是否序列 /// </summary> public bool IsSequence { get { return _isSequence; } } #endregion } public class VisitorItem { public VisitorItem(PropertyInfo PropertyInfo, AliasAttribute AliasAttribute, SelectAttribute SelectAttribute) { this.PropertyInfo = PropertyInfo; this.AliasAttribute = AliasAttribute; this.SelectAttribute = SelectAttribute; } public PropertyInfo PropertyInfo { get; set; } public AliasAttribute AliasAttribute { get; set; } public SelectAttribute SelectAttribute { get; set; } } public class DynamicVisitor<TResult> : IEnumerable<VisitorItem> { private static object lockHelper = new object(); private static DynamicVisitor<TResult> visits = null; public static DynamicVisitor<TResult> Instance { get { if (visits == null) { lock (lockHelper) { if (visits == null) { visits = new DynamicVisitor<TResult>(); } } } return visits; } } public string TableName { get; set; } private VisitorItem[] tuples; private Dictionary<string, int> PropertyNames; private Dictionary<string, int> AliasNames; public DynamicVisitor() { if (!string.IsNullOrEmpty(TableName)) return; PropertyNames = new Dictionary<string, int>(); AliasNames = new Dictionary<string, int>(); var _type = typeof(TResult); var properties = _type.GetProperties(); tuples = new VisitorItem[properties.Length]; for (var i = 0; i < properties.Length; i++) { var pi = properties[i]; tuples[i] = new VisitorItem(pi, pi.GetCustomAttributes(true).OfType<AliasAttribute>().FirstOrDefault(), pi.GetCustomAttributes(true).OfType<SelectAttribute>().FirstOrDefault()); PropertyNames.Add(pi.Name.ToUpper(), i); AliasNames.Add(tuples[i].AliasAttribute != null ? tuples[i].AliasAttribute.AliasName.ToUpper() : pi.Name.ToUpper(), i); } var ta = _type.GetCustomAttributes(true).OfType<TableAttribute>().FirstOrDefault(); TableName = ta == null ? _type.Name : ta.TableName; } public int Count { get { return tuples.Length; } } public VisitorItem this[string propertyName] { get { return tuples[PropertyNames[propertyName.ToUpper()]]; } }public object GetValue(TResult result, string propertyName) { return this[propertyName.ToUpper()].PropertyInfo.GetValue(result, null); } public void SetValue(TResult result, string aliasName, object value) { try { var pi = tuples[AliasNames[aliasName.ToUpper()]].PropertyInfo; if (value == DBNull.Value) value = null; if (pi.GetType() == typeof(bool)) { bool success; if (bool.TryParse(value.ToString(), out success)) { value = success; } else { value = int.Parse(value.ToString()) != 0; } } pi.SetValue(result, value, null); } catch (Exception ex) { throw new Exception(aliasName, ex); } } public IEnumerator<VisitorItem> GetEnumerator() { for (int i = 0; i < Count; i++) { yield return tuples[i]; } } IEnumerator IEnumerable.GetEnumerator() { return tuples.GetEnumerator(); } } }
既然微软已经为C#准备了这么多metadata的数据,何不拿来一用。
企业应用的说法太可笑,世界上还是大、中型公司为数众多,轻量级应用遍布,当大牛们在超大级公司选工作的同时,IT码农们还在为了找工作、涨薪而穷心竭力
合适的才是最好的,理想也是实现价值

浙公网安备 33010602011771号