Beispiel #1
0
        /// <summary>
        ///  将对象赋值
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="rval">表单集合字符串</param>
        /// <param name="model">从数据库查询的实体对象</param>
        /// <returns></returns>
        public static T ConvertToModel <T>(this string rval, T model)
        {
            Type modelType = model.GetType();

            PropertyInfo[] props        = modelType.GetProperties();//获取此对象的,字段集合
            object         propertValue = null;
            string         value        = String.Empty;

            foreach (PropertyInfo item in props)
            {
                if (rval.ValidationKey(item.Name))
                {
                    value        = rval.GetFromValue(item.Name);
                    propertValue = GetVal(propertValue, value, item.PropertyType.Name);
                    if (item.PropertyType.Name == "Double")//如果对象类型是Double数字,等于0则不予赋值
                    {
                        if (propertValue.ToString() != "0")
                        {
                            PropertyInfoX.SetValue(model, item.Name, propertValue);//给字段赋值
                        }
                    }
                    else
                    {
                        PropertyInfoX.SetValue(model, item.Name, propertValue);//给字段赋值
                    }
                }
            }
            return(model);
        }
Beispiel #2
0
        private static Boolean SetControlValue(Control control, Object value)
        {
            TypeX         tx   = control.GetType();
            String        name = tx.GetCustomAttributeValue <ControlValuePropertyAttribute, String>();
            PropertyInfoX pix  = null;

            if (!String.IsNullOrEmpty(name))
            {
                pix = PropertyInfoX.Create(tx.Type, name);
            }
            if (pix == null)
            {
                pix = PropertyInfoX.Create(tx.Type, "Value");
            }
            if (pix == null)
            {
                pix = PropertyInfoX.Create(tx.Type, "Text");
            }
            if (pix != null)
            {
                if (value == null && pix.Type.IsValueType)
                {
                    return(false);
                }
                pix.SetValue(control, value);
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        static Boolean GetControlValue(ControlBase control, out Object value)
        {
            TypeX         tx   = control.GetType();
            String        name = tx.GetCustomAttributeValue <ControlValuePropertyAttribute, String>();
            PropertyInfoX pix  = null;

            if (!String.IsNullOrEmpty(name))
            {
                pix = PropertyInfoX.Create(tx.BaseType, name);
            }
            if (pix == null)
            {
                pix = PropertyInfoX.Create(tx.BaseType, "Value");
            }
            if (pix == null)
            {
                pix = PropertyInfoX.Create(tx.BaseType, "Text");
            }
            if (pix != null)
            {
                value = pix.GetValue(control);
                return(true);
            }

            value = null;
            return(false);
        }
Beispiel #4
0
        public IEnumerable <T> BindModel <T>(object source)
        {
            DataTable dt = (DataTable)source;
            IEnumerable <DataColumn> dclist = dt.Columns.Cast <DataColumn>();
            List <T> modellist = new List <T>();

            foreach (DataRow dr in dt.Rows)
            {
                TypeX typex         = TypeX.Create(typeof(T));
                T     modelInstance = (T)typex.CreateInstance();
                //T modelInstance = (T)Activator.CreateInstance(typeof(T));
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    DataColumn dc = dclist.Where(item => string.Compare(property.Name, item.ColumnName, true) == 0).FirstOrDefault();

                    if (null != dc)
                    {
                        PropertyInfoX propx = PropertyInfoX.Create(property);
                        propx.SetValue(modelInstance, dr[dc.ColumnName]);

                        //property.SetValue(modelInstance, Convert.ChangeType(dr[dc.ColumnName], property.PropertyType),null);
                    }
                }
                modellist.Add(modelInstance);
            }

            return(modellist);
        }
Beispiel #5
0
        static String GetCustomAttributeValue <TAttribute>(MemberInfo member, String name)
        {
            TAttribute att = AttributeX.GetCustomAttribute <TAttribute>(member, true);

            if (att == null)
            {
                return(null);
            }

            PropertyInfoX pix = PropertyInfoX.Create(typeof(TAttribute), name);

            if (pix == null)
            {
                return(null);
            }

            return((String)pix.GetValue(att));
        }
Beispiel #6
0
        /// <summary>设置控件的ToolTip提示信息</summary>
        /// <param name="field"></param>
        /// <param name="control"></param>
        protected virtual void SetToolTip(FieldItem field, Control control)
        {
            if (field == null || control == null)
            {
                return;
            }

            String toolTip = field.DisplayName;

            if (field.IsNullable)
            {
                toolTip = String.Format("请填写{0}!", toolTip);
            }
            else
            {
                toolTip = String.Format("必须填写{0}!", toolTip);
            }

            //if (control is Label) toolTip = null;
            if (control is Label)
            {
                return;
            }

            if (control is WebControl)
            {
                var wc = control as WebControl;

                // 设置ToolTip
                if (String.IsNullOrEmpty(wc.ToolTip) && !String.IsNullOrEmpty(toolTip))
                {
                    wc.ToolTip = toolTip;
                }
            }
            else
            {
                var pix = PropertyInfoX.Create(control.GetType(), "ToolTip");
                if (pix != null && String.IsNullOrEmpty((String)pix.GetValue(control)))
                {
                    pix.SetValue(control, toolTip);
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// [OLD Code]返回一个新创建的实体对象,并给各个字段赋值
        /// </summary>
        /// <param name="typeName">完整实体名称, [命名空间.实体名称]</param>
        /// <param name="rval">表单集合字符串</param>
        /// <returns></returns>
        public static object GetFormEntity(this string rval, string typeName)
        {
            typeName = string.Format("{0}.{1}", ConfigHelper.GetAppSettings("EntitySpaceName"), typeName);
            TypeX  EntityType = TypeX.Create(TypeX.GetType(typeName, true));   //根据类的完整名称建立类的类型,用于动态创建类 如: Clump.Mobile.Models.NewsInfo
            Object objEntity  = EntityType.CreateInstance();                   //动态建立类的对象 实体类的对象Object objEntity = EntityType.CreateInstance(true);意思是在创建实体对象时 A a = new A(true)

            PropertyInfo[] props        = objEntity.GetType().GetProperties(); //获取此对象的,字段集合
            object         propertValue = String.Empty;

            foreach (PropertyInfo item in props)
            {
                propertValue = rval.GetFromValue(item.Name);
                if (!"".Equals(propertValue))
                {
                    //根据字段类型,转换格式
                    switch ((PropertyInfoX.Create(EntityType, item.Name)).Property.PropertyType.Name)
                    {
                    case "String": propertValue = Convert.ToString(propertValue); break;

                    case "Int64": propertValue = Convert.ToInt64(propertValue); break;

                    case "Int32": propertValue = Convert.ToInt32(propertValue); break;

                    case "Int16": propertValue = Convert.ToInt16(propertValue); break;

                    case "Byte": propertValue = Convert.ToByte(propertValue); break;

                    case "Single": propertValue = Convert.ToSingle(propertValue); break;

                    case "Double": propertValue = Convert.ToDouble(propertValue); break;

                    case "Boolean": propertValue = Convert.ToBoolean(propertValue); break;

                    case "DateTime": propertValue = Convert.ToDateTime(propertValue); break;

                    default: break;
                    }
                    PropertyInfoX.SetValue(objEntity, item.Name, propertValue);//给字段赋值
                }
            }
            PropertyInfoX.SetValue(objEntity, "CreateTime", DateTime.Now);//给CreateTime字段赋值,添加时间字段是每个数据表标配(未解决:MySql配置,暂时不能默认添加当前时间)
            return(objEntity);
        }
Beispiel #8
0
        /// <summary>查找控件的事件</summary>
        /// <param name="control"></param>
        /// <param name="eventName"></param>
        /// <returns></returns>
        public static Delegate FindEventHandler(Control control, String eventName)
        {
            if (control == null)
            {
                return(null);
            }
            if (String.IsNullOrEmpty(eventName))
            {
                return(null);
            }

            var pix = PropertyInfoX.Create(control.GetType(), "Events");

            if (pix == null)
            {
                return(null);
            }

            EventHandlerList list = pix.GetValue(control) as EventHandlerList;

            if (list == null)
            {
                return(null);
            }

            var fix = FieldInfoX.Create(control.GetType(), eventName);

            if (fix == null && !eventName.StartsWith("Event", StringComparison.Ordinal))
            {
                fix = FieldInfoX.Create(control.GetType(), "Event" + eventName);
            }
            if (fix == null)
            {
                return(null);
            }

            return(list[fix.GetValue(control)]);
        }
Beispiel #9
0
        void CompressContent(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (!(app.Context.CurrentHandler is System.Web.UI.Page) || app.Request["HTTP_X_MICROSOFTAJAX"] != null)
            {
                return;
            }

            // 如果已经写入头部,这里就不能压缩了
            var pix = PropertyInfoX.Create(app.Response.GetType(), "HeadersWritten");

            if (pix != null && (Boolean)pix.GetValue(app.Response))
            {
                return;
            }
            // .net 2.0没有HeadersWritten
            //if (PropertyInfoX.GetValue<Boolean>(app.Response, "HeadersWritten")) return;

            //压缩
            String url   = app.Request.Url.OriginalString.ToLower();
            String files = Config.GetConfig <String>("NewLife.CommonEntity.CompressFiles", ".aspx,.axd,.js,.css");

            if (files.ToLower().Split(",", ";", " ").Any(t => url.Contains(t)))
            {
                //是否支持压缩协议
                if (IsEncodingAccepted(GZIP))
                {
                    app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
                    SetEncoding(GZIP);
                }
                else if (IsEncodingAccepted(DEFLATE))
                {
                    app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
                    SetEncoding(DEFLATE);
                }
            }
        }
Beispiel #10
0
        /// <summary>
        ///  将对象赋值
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="formModel">表单获取的实体对象</param>
        /// <param name="model">从数据库查询的实体对象</param>
        /// <returns></returns>
        public static T ConvertToModel <T>(T formModel, T model)
        {
            Type formModelType = formModel.GetType();
            Type modelType     = model.GetType();

            PropertyInfo[] props        = modelType.GetProperties();     //获取此对象的,字段集合
            PropertyInfo[] formProps    = formModelType.GetProperties(); //获取此对象的,字段集合
            object         propertValue = null;
            string         value        = String.Empty;

            for (int i = 0; i < props.Length; i++)
            {
                if (props[i].Name.Equals(formProps[i].Name) && props[i].CanWrite == true)
                {
                    propertValue = PropertyInfoX.GetValue(formModel, formProps[i].Name);
                    if (propertValue != null)
                    {
                        PropertyInfoX.SetValue(model, props[i].Name, propertValue);//给字段赋值
                    }
                }
            }
            return(model);
        }
Beispiel #11
0
        Type GetItemType(String name)
        {
            if (String.IsNullOrEmpty(name) || EntityType == null)
            {
                return(null);
            }

            PropertyInfoX pi = PropertyInfoX.Create(EntityType, name);

            if (pi != null)
            {
                return(pi.Type);
            }

            FieldInfoX fi = FieldInfoX.Create(EntityType, name);

            if (fi != null)
            {
                return(fi.Type);
            }

            return(null);
        }
Beispiel #12
0
        /// <summary>
        /// 把实体成员的值设置到控件上
        /// </summary>
        /// <param name="field"></param>
        /// <param name="control"></param>
        /// <param name="canSave"></param>
        protected virtual void SetFormItem(FieldItem field, System.Web.UI.Control control, Boolean canSave)
        {
            if (field == null || control == null)
            {
                return;
            }

            String toolTip = String.IsNullOrEmpty(field.Description) ? field.Name : field.Description;

            if (field.IsNullable)
            {
                toolTip = String.Format("请填写{0}!", toolTip);
            }
            else
            {
                toolTip = String.Format("必须填写{0}!", toolTip);
            }

            if (control is Label)
            {
                toolTip = null;
            }

            if (control is ExtAspNet.ControlBase)
            {
                ExtAspNet.ControlBase wc = control as ExtAspNet.ControlBase;

                // 设置ToolTip
                //if (String.IsNullOrEmpty(wc.ToolTip) && !String.IsNullOrEmpty(toolTip)) wc.ToolTip = toolTip;

                //// 必填项
                //if (!field.IsNullable) SetNotAllowNull(field, control, canSave);

                // 设置只读,只有不能保存时才设置,因为一般都不是只读,而前端可能自己设置了控件为只读,这种情况下这里再设置就会修改前端的意思
                if (!canSave)
                {
                    if (wc is ExtAspNet.TextBox)
                    {
                        (wc as TextBox).Enabled = !canSave;
                    }
                    else
                    {
                        wc.Enabled = canSave;
                    }
                }

                // 分控件处理
                if (wc is TextBox || wc is DatePicker)
                {
                    SetFormItemTextBox(field, wc as RealTextField, canSave);
                }
                else if (wc is Label)
                {
                    SetFormItemLabel(field, wc as Label, canSave);
                }
                else if (wc is RadioButton)
                {
                    SetFormItemRadioButton(field, wc as RadioButton, canSave);
                }
                else if (wc is CheckBox)
                {
                    SetFormItemCheckBox(field, wc as CheckBox, canSave);
                }
                else if (wc is DropDownList)
                {
                    SetFormItemListControl(field, wc as DropDownList, canSave);
                }
                else
                {
                    SetControlValue(control as ControlBase, Entity[field.Name]);
                }
            }
            else
            {
                SetControlValue(control as ControlBase, Entity[field.Name]);

                PropertyInfoX pix = PropertyInfoX.Create(control.GetType(), "ToolTip");
                if (pix != null && String.IsNullOrEmpty((String)pix.GetValue(control)))
                {
                    pix.SetValue(control, toolTip);
                }
            }
        }
Beispiel #13
0
 static PropertyInfoX[] GetProperties(Type type)
 {
     return(cache2.GetItem(type, item => item.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => !p.Name.EqualIgnoreCase("Item")).Select(p => PropertyInfoX.Create(p)).ToArray()));
 }