示例#1
0
        /// <summary>
        /// 获取已赋值的属性信息(属性名、属性类型、属性值)
        /// </summary>
        /// <param name="paramModel">Model对象</param>
        /// <returns></returns>
        public List <ComMDAttribute> GetPropertyListNoFormatValue(object paramModel)
        {
            object param = paramModel;

            List <ComMDAttribute> resultList = new List <ComMDAttribute>();

            Type t = param.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                ComMDAttribute ComMDAttribute = new ComMDAttribute();
                if (pi.GetValue(param, null) != null)
                {
                    //属性值
                    ComMDAttribute.Value = pi.GetValue(param, null).ToString();
                    //属性名
                    ComMDAttribute.Name = pi.Name;
                    //属性类型
                    ComMDAttribute.Type = pi.PropertyType.Name;
                    resultList.Add(ComMDAttribute);
                }
            }
            return(resultList);
        }
示例#2
0
        /// <summary>
        /// 获取已赋值的属性信息(属性名、属性类型、属性值)
        /// 根据SQL文语法规则格式化属性值
        /// </summary>
        /// <param name="paramModel">Model对象</param>
        /// <returns></returns>
        public List <ComMDAttribute> GetPropertyListFormatValue(object paramModel)
        {
            object param = paramModel;

            //Sting型
            Type str = typeof(System.String);
            //Int32型
            Type int32 = typeof(System.Int32);
            //Int64型
            Type int64 = typeof(System.Int64);
            //Decimal型
            Type dec = typeof(System.Decimal);
            //DateTime型
            Type dateTime = typeof(System.DateTime);
            //Boolean型
            Type boolean = typeof(System.Boolean);
            //字节数组
            Type byteArray = typeof(System.Byte[]);

            //Int32型
            Type int32N = typeof(System.Nullable <Int32>);
            //Int64型
            Type int64N = typeof(System.Nullable <Int64>);
            //Decimal型
            Type decN = typeof(System.Nullable <Decimal>);
            //DateTime型
            Type dateTimeN = typeof(System.Nullable <DateTime>);
            //Boolean型
            Type booleanN = typeof(System.Nullable <Boolean>);

            //定义Model属性List
            List <ComMDAttribute> resultList = new List <ComMDAttribute>();

            //获取Model类型
            Type t = param.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                //定义Model属性
                ComMDAttribute ComMDAttribute = new ComMDAttribute();
                //判断属性值是否为空(属性值为空不处理)
                if (pi.GetValue(param, null) != null)
                {
                    //Sting型
                    if (str.Equals(pi.PropertyType))
                    {
                        //把属性值用单引号括起来
                        ComMDAttribute.Value = String.Format("'{0}'", pi.GetValue(param, null));
                    }
                    //int32型
                    else if (int32.Equals(pi.PropertyType))
                    {
                        //属性值保持原样
                        ComMDAttribute.Value = pi.GetValue(param, null).ToString();
                    }
                    //int64型
                    else if (int64.Equals(pi.PropertyType) || int64N.Equals(pi.PropertyType))
                    {
                        //属性值保持原样
                        ComMDAttribute.Value = pi.GetValue(param, null).ToString();
                    }
                    //decimal型
                    else if (dec.Equals(pi.PropertyType) || decN.Equals(pi.PropertyType))
                    {
                        //属性值保持原样
                        ComMDAttribute.Value = pi.GetValue(param, null).ToString();
                    }
                    //DateTime型
                    else if (dateTime.Equals(pi.PropertyType) || dateTimeN.Equals(pi.PropertyType))
                    {
                        //把属性值用单引号括起来
                        ComMDAttribute.Value = String.Format("'{0}'", pi.GetValue(param, null));
                    }
                    //Boolean型
                    else if (boolean.Equals(pi.PropertyType) || booleanN.Equals(pi.PropertyType))
                    {
                        //把属性值用单引号括起来
                        ComMDAttribute.Value = String.Format("'{0}'", pi.GetValue(param, null));
                    }
                    //byte[]
                    else if (byteArray.Equals(pi.PropertyType))
                    {
                        ComMDAttribute.Value = System.Convert.ToBase64String((byte[])pi.GetValue(param, null));
                    }
                    //其他类型
                    else
                    {
                        //属性值保持原样
                        ComMDAttribute.Value = pi.GetValue(param, null);
                    }

                    //属性名
                    ComMDAttribute.Name = pi.Name;
                    //属性类型
                    ComMDAttribute.Type = pi.PropertyType.Name;
                    //把Model属性信息追加到属性List内
                    resultList.Add(ComMDAttribute);
                }
            }


            return(resultList);
        }