资源简介
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
评论
共有 条评论