コード例 #1
0
        /// <summary>
        /// 把实体对象数据填充到表现层容器,实体对象一般是IDictionary或者实体类对象
        /// </summary>
        /// <param name="container">数据表现层容器(一般是Form或者Div)</param>
        /// <param name="value">单条数据实体,一般是IDictionary或者实体类对象</param>
        /// <returns>从表现层容器取值后的实体对象</returns>
        /// <remarks>
        /// 创建人:qipengfei
        /// 创建日期:2017-08-25
        /// 修改人:
        /// 修改日期:yyyy-mm-dd
        /// 修改备注:无
        /// 版本:1.0
        /// </remarks>
        internal static void SetContainerValue(Control.ControlCollection controls, Object value)
        {
            if (value == null || controls == null)
            {
                return;
            }// if

            #region 绑定控件值
            foreach (Control control in controls)
            {
                //控件名称
                string controlName = control.AccessibleName;
                if (!string.IsNullOrEmpty(controlName))
                {
                    if (value is IDictionary <string, object> )
                    {
                        var dict = value as IDictionary <string, object>;
                        if (dict.ContainsKey(controlName))
                        {
                            SetValue(control, dict[controlName]);
                            continue;
                        }
                    }// if
                    else if (value is IDictionary)
                    {
                        if (((IDictionary)value).Contains(controlName))
                        {
                            SetValue(control, ((IDictionary)value)[controlName]);
                            continue;
                        } // if
                    }     // else if
                    else
                    {
                        Type         objType  = value.GetType();
                        FastType     fastType = FastType.Get(objType);
                        FastProperty prop     = fastType.GetGetter(controlName);
                        if (prop != null)
                        {
                            //类型转换
                            SetValue(control, prop.GetValue(value));
                            continue;
                        } // if
                    }     // else
                }         // if

                // if we didn't find it here recurse into child containers
                if (control.HasChildren)
                {
                    SetContainerValue(control.Controls, value);
                }
            }// foreach
            #endregion
        }
コード例 #2
0
        /// <summary>
        /// 将控件的值映射到实体中对应的属性上
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="control"></param>
        /// <param name="obj"></param>
        /// <remarks>
        /// 创建人:qipengfei
        /// 创建日期:2017-08-24
        /// 修改人:
        /// 修改日期:yyyy-mm-dd
        /// 修改备注:无
        /// 版本:1.0
        /// </remarks>
        internal static T EvaluateRecuceive <T>(Control control, T obj)
            where T : class, new()
        {
            if (control is Label ||
                control is LinkLabel ||
                control is Button ||
                control is Panel ||
                control is GroupBox)
            {
                return(obj);
            } // if
            else
            {
                //控件名称
                string controlName = control.AccessibleName;
                //获取相应类型控件的值
                Object value = Evaluate(control);

                //设置值
                if (value != null && controlName != null)
                {
                    #region 设置值

                    if (obj is IDictionary <String, Object> )
                    {
                        if (((IDictionary <String, Object>)obj).ContainsKey(controlName))
                        {
                            ((IDictionary <String, Object>)obj)[controlName] = value;
                        }// if
                        else
                        {
                            ((IDictionary <String, Object>)obj).Add(controlName, value);
                        }// else
                    }
                    else if (obj is IDictionary)
                    {
                        if (((IDictionary)obj).Contains(controlName))
                        {
                            ((IDictionary)obj).Remove(controlName);
                        }// if
                        else
                        {
                            ((IDictionary)obj).Add(controlName, value);
                        } // else
                    }     // else if
                    else
                    {
                        Type         objType  = obj.GetType();
                        FastType     fastType = FastType.Get(objType);
                        FastProperty prop     = fastType.GetGetter(controlName);

                        if (prop != null)
                        {
                            try
                            {
                                //类型转换
                                value = ObjectHelper.ToType(prop.Type, value);
                                prop.SetValue(obj, value);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("类型转换出错。\n字段名:" + controlName + ";字段值:" + value + "。\n" + ex.ToString());
                            }
                        } // if
                    }     // else
                    #endregion
                }
            }
            return(obj);
        }