CC.Data/Helper/EntityHelper.cs

(开头部分) 10KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Reflection;

using CC.Data.Common;

namespace CC.Data.Helper
{
    public class EntityHelper
    {

        #region 基本变量


        //将类型与该类型所有的可写且未被忽略属性之间建立映射
        private static Dictionary<Type, Dictionary<string, PropertyInfo>> propertyMappings = new Dictionary<Type, Dictionary<string, PropertyInfo>>();
        #endregion

        #region 基本方法
        /// <summary>
        /// 获取一个对象实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T GetEntity<T>(SqlDataReader dr) where T : class,new()
        {
            T model = new T();
            if (!propertyMappings.ContainsKey(typeof(T)))
            {
                GenerateTypePropertyMapping(typeof(T));
            }
            Dictionary<string, PropertyInfo> properties = propertyMappings[typeof(T)];

            foreach (KeyValuePair<string, PropertyInfo> item in properties)
            {
                //如果对应的属性名出现在数据源的列中则获取值并设置给对应的属性
                if (dr[item.Key] != null)
                {
                    item.Value.SetValue(model, GetValueFromObject(dr[item.Key], item.Value.PropertyType), null);
                }
            }
            return model;
        }


        /// <summary>
        /// 获取属性字段的特性
        /// </summary>
        public static List<FieldAttribute> GetFieldAttributeList<T>() where T : class,new()
        {
            List<FieldAttribute> list = (from p in typeof(T).GetProperties()
                                         where p.GetCustomAttributes(true).Any<object>(a => a.GetType() == typeof(FieldAttribute))
                                         select p.GetCustomAttributes(true)[0] as FieldAttribute).ToList<FieldAttribute>();
            return list;
        }
        /// <summary>
后面还有 224 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。