Esempio n. 1
0
        /// <summary>数据表转模型列表。普通反射,便于DAL查询后转任意模型列表</summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IEnumerable <T> ReadModels <T>()
        {
            // 可用属性
            var pis = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var dic = pis.ToDictionary(e => SerialHelper.GetName(e), e => e, StringComparer.OrdinalIgnoreCase);

            foreach (var row in Rows)
            {
                var model = (T)typeof(T).CreateInstance();
                for (var i = 0; i < row.Length; i++)
                {
                    // 扩展赋值,或 反射赋值
                    if (dic.TryGetValue(Columns[i], out var pi) && pi.CanWrite)
                    {
                        var val = row[i].ChangeType(pi.PropertyType);
                        if (model is IExtend ext)
                        {
                            ext[Columns[i]] = val;
                        }
                        else
                        {
                            pi.SetValue(model, val, null);
                        }
                    }
                }

                yield return(model);
            }
        }
Esempio n. 2
0
        /// <summary>写入模型列表</summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        public void WriteModels <T>(IEnumerable <T> models)
        {
            // 可用属性
            var pis = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            pis = pis.Where(e => e.PropertyType.GetTypeCode() != TypeCode.Object).ToArray();

            Rows = new List <Object[]>();
            foreach (var item in models)
            {
                // 头部
                if (Columns == null)
                {
                    Columns = pis.Select(e => SerialHelper.GetName(e)).ToArray();
                    Types   = pis.Select(e => e.PropertyType).ToArray();
                }

                var row = new Object[Columns.Length];
                for (var i = 0; i < row.Length; i++)
                {
                    // 反射取值
                    if (pis[i].CanRead)
                    {
                        row[i] = pis[i].GetValue(item, null);
                    }
                }
                Rows.Add(row);
            }
        }
Esempio n. 3
0
        /// <summary>目标匿名参数对象转为字典</summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static IDictionary <String, Object> ToDictionary(this Object target)
        {
            //!! 即使传入为空,也返回字典,而不是null,避免业务层需要大量判空
            //if (target == null) return null;
            if (target is IDictionary <String, Object> dic)
            {
                return(dic);
            }

            dic = new NullableDictionary <String, Object>(StringComparer.OrdinalIgnoreCase);
            if (target != null)
            {
                // 修正字符串字典的支持问题
                if (target is IDictionary dic2)
                {
                    foreach (DictionaryEntry item in dic2)
                    {
                        dic[item.Key + ""] = item.Value;
                    }
                }
                else
                {
                    foreach (var pi in target.GetType().GetProperties(true))
                    {
                        var name = SerialHelper.GetName(pi);
                        dic[name] = target.GetValue(pi);
                    }
                }
            }

            return(dic);
        }
Esempio n. 4
0
        /// <summary>从实例公有属性映射到配置树</summary>
        /// <param name="section"></param>
        /// <param name="model"></param>
        public static void MapFrom(this IConfigSection section, Object model)
        {
            if (section == null)
            {
                return;
            }

            // 反射公有实例属性
            foreach (var pi in model.GetType().GetProperties(true))
            {
                if (!pi.CanRead || !pi.CanWrite)
                {
                    continue;
                }
                //if (pi.GetIndexParameters().Length > 0) continue;
                //if (pi.GetCustomAttribute<IgnoreDataMemberAttribute>(false) != null) continue;
                //if (pi.GetCustomAttribute<XmlIgnoreAttribute>() != null) continue;

                var name = SerialHelper.GetName(pi);
                if (name.EqualIgnoreCase("ConfigFile", "IsNew"))
                {
                    continue;
                }

                // 名称前面加上命名空间
                var cfg = section.GetOrAddChild(name);

                // 反射获取属性值
                var val = pi.GetValue(model, null);
                var att = pi.GetCustomAttribute <DescriptionAttribute>();
                cfg.Comment = att?.Description;
                if (cfg.Comment.IsNullOrEmpty())
                {
                    var att2 = pi.GetCustomAttribute <DisplayNameAttribute>();
                    cfg.Comment = att2?.DisplayName;
                }

                //!! 即使模型字段值为空,也必须拷贝,否则修改设置时,无法清空某字段
                //if (val == null) continue;

                // 分别处理基本类型、数组类型、复杂类型
                if (pi.PropertyType.GetTypeCode() != TypeCode.Object)
                {
                    cfg.SetValue(val);
                }
                else if (pi.PropertyType.As <IList>())
                {
                    if (val is IList list)
                    {
                        MapArray(section, cfg, list, pi.PropertyType.GetElementTypeEx());
                    }
                }
                else
                {
                    // 递归映射
                    MapFrom(cfg, val);
                }
            }
        }
Esempio n. 5
0
        /// <summary>映射配置树到实例公有属性</summary>
        /// <param name="section">数据源</param>
        /// <param name="model">模型</param>
        internal static void MapTo(this IConfigSection section, Object model)
        {
            if (section == null || section.Childs == null || section.Childs.Count == 0 || model == null)
            {
                return;
            }

            // 反射公有实例属性
            foreach (var pi in model.GetType().GetProperties(true))
            {
                if (!pi.CanRead || !pi.CanWrite)
                {
                    continue;
                }
                //if (pi.GetIndexParameters().Length > 0) continue;
                //if (pi.GetCustomAttribute<IgnoreDataMemberAttribute>(false) != null) continue;
                //if (pi.GetCustomAttribute<XmlIgnoreAttribute>() != null) continue;

                var name = SerialHelper.GetName(pi);
                if (name.EqualIgnoreCase("ConfigFile", "IsNew"))
                {
                    continue;
                }

                var cfg = section.Childs?.FirstOrDefault(e => e.Key.EqualIgnoreCase(name));
                if (cfg == null)
                {
                    continue;
                }

                // 分别处理基本类型、数组类型、复杂类型
                if (pi.PropertyType.GetTypeCode() != TypeCode.Object)
                {
                    pi.SetValue(model, cfg.Value.ChangeType(pi.PropertyType), null);
                }
                else if (cfg.Childs != null)
                {
                    if (pi.PropertyType.As <IList>())
                    {
                        if (pi.PropertyType.IsArray)
                        {
                            MapArray(cfg, model, pi);
                        }
                        else
                        {
                            MapList(cfg, model, pi);
                        }
                    }
                    else
                    {
                        // 复杂类型需要递归处理
                        var val = pi.GetValue(model, null);
                        if (val == null)
                        {
                            // 如果有无参构造函数,则实例化一个
                            var ctor = pi.PropertyType.GetConstructor(new Type[0]);
                            if (ctor != null)
                            {
                                val = ctor.Invoke(null);
                                pi.SetValue(model, val, null);
                            }
                        }

                        // 递归映射
                        if (val != null)
                        {
                            MapTo(cfg, val);
                        }
                    }
                }
            }
        }