資源簡介
ASP.NET MVC5+EasyUI企業開發框架源碼
代碼片段和文件信息
/*************************************************************************
?*?文件名稱?:baseEntity.cs??????????????????????????
?*?描述說明?:定義實體基類
?*?
?**************************************************************************/
using?System;
using?System.Collections.Concurrent;
using?System.Collections.Generic;
using?System.Dynamic;
using?System.Linq;
using?JTS.Utils;
namespace?JTS.Core
{
????///?
????///?框架實體基類
????///?
????public?class?baseEntity?:?ICloneable?IComparable?IDisposable
????{
????????///?
????????///?定義屬性緩存對象
????????///?
????????private?static?readonly?ConcurrentDictionary>>?_cachedAtrributes?=?new?ConcurrentDictionary>>();
????????///?
????????///?獲取實體某個自定義特性的所有的字段名稱
????????///?
????????///?TEntity實體類型
????????///?TAttribute自定義特性類型
????????///?list
????????public?static?List?GetAttributeFields()
????????{
????????????var?key?=?typeof(TAttribute).Name;
????????????var?thisAttributes?=?_cachedAtrributes.GetOrAdd(typeof(TEntity)?BuildAtrributeDictionary);
????????????return?thisAttributes.ContainsKey(key)???thisAttributes[typeof(TAttribute).Name]?:?new?List();
????????}
????????private?static?Dictionary>?BuildAtrributeDictionary(Type?TEntity)
????????{
????????????var?result?=?new?Dictionary>();
????????????foreach?(var?property?in?TEntity.GetProperties())
????????????{
????????????????var?attributes?=?property.GetCustomAttributes(typeof(Attribute)?true)?as?Attribute[];
????????????????if?(attributes?!=?null)
????????????????????foreach?(var?key?in?attributes.Select(attr?=>?attr.GetType().Name))
????????????????????{
????????????????????????if?(!result.ContainsKey(key))
????????????????????????????result.Add(key?new?List());
????????????????????????result[key].Add(property.Name);
????????????????????}
????????????}
????????????return?result;
????????}
????????///?
????????///?擴展實體對象的屬性值
????????///?
????????///?實體對象,包含實體屬性名和屬性值
????????///?返回dynamic
????????public?dynamic?Extend(object?obj)
????????{
????????????var?expando?=?(IDictionaryject>)new?Expandoobject();
????????????EachHelper.EachobjectProperty(this?(i?name?value)?=>
????????????{
????????????????expando.Add(name?value);
????????????});
????????????EachHelper.EachobjectProperty(obj?(i?name?value)?=>
????????????{
????????????????expando[name]?=?value;
????????????});
????????????return?expando;
????????}
????????///?
????????///?獲取實體屬性值
????????///?
????????///?屬性名
????????///?object
????????public?object?GetValue
評論
共有 條評論