Exemplo n.º 1
0
        /// <summary>
        /// 根据过滤器获取查询字符串
        /// </summary>
        /// <typeparam name="T">Filter</typeparam>
        /// <param name="t">过滤器实体</param>
        /// <returns></returns>
        public static string GetQueryString <T>(this T t) where T : Filter.IFilter
        {
            string strRet = "";

            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo item in properties)
            {
                FilterAttribute remarkAttribute = (FilterAttribute)item.GetCustomAttribute(typeof(FilterAttribute));
                string          strValue        = item.GetValue(t) + "";
                if (remarkAttribute != null && strValue != "")
                {
                    if (remarkAttribute.FilterType == Filter_Type.SqlFilter)
                    {
                        strRet += string.Format(" and {0}", strValue);
                    }
                    else if (remarkAttribute.FilterType == Filter_Type.QueryIn)
                    {
                        strRet += string.Format(" and {0} {1} {2}", item.Name, remarkAttribute.GetOperator(ref strValue), strValue);
                    }
                    else
                    {
                        strRet += string.Format(" and {0} {1} '{2}'", item.Name, remarkAttribute.GetOperator(ref strValue), strValue);
                    }
                }
            }
            return(strRet);
        }