c # reflection - recording one kind of assembly methods sql

 Type type = model.GetType(); //获取类型
 System.Reflection.PropertyInfo propertyInfo = type.GetProperty(textType);
 decimal value = (decimal)propertyInfo.GetValue(model, null);

Add the type of reflection that defines a public method

   public static  object ActionHandler(string fullName, string assemblyName, object[] 
   prams, string method) {
            string path = fullName + "," + assemblyName;//命名空间.类型名,程序集
            Type p = Type.GetType(path,false,true);//加载类型,是否抛出异常,是否区分字符串大小写
            try
            {
                object obj = Activator.CreateInstance(p, prams);//根据类型创建实例
                                                                //反射方法,不区分大小写
                MethodInfo methods = p.GetMethod(method, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
             //判断当前对象是否存在指定的方法 obj指实例化对象,null指IsMoveing()的参数
             // (bool)menthod.Invoke(obj,null)
            // method.Invoke( obj, arrParam ); 
              //obj为调用方法所属的类型实例,如果调用的为静态方法,可设置为null;arrParam为方法参数数组
                return methods.Invoke(obj, null);
            }
           catch(Exception e) {
                var s = e.Message;
                return null;
            }
        }

The first step a new value to store objects

/// <summary>
        /// 生成参数
        /// </summary>
        /// <param name="name">字段名(列名)</param>
        /// <param name="val">参数值(若算法是in,多个参数值传入逗号隔开的字符串  "val1,val2,val3")</param>
        /// <param name="weather">是否拼接,默认为true</param>
        /// <param name="cal">算法(=、>=等)注:like算法如包含多个字段,参数名也用逗号隔开 "a1,a2,a3"</param>
        /// <param name="blur">1表示前面模糊,2表示后模糊,3表示前后模糊,其余的数字表示精确查询(默认项)</param>
        /// <returns></returns>
        public static NzSql MakeParam(string name,object val,bool weather=true,string cal="=",int blur=0)
        {
            NzSql param = new NzSql
            {
                ParamValue = val,
                ParamName = name,
                Blur = blur,
                ParamCal = cal,
                ParamBool = weather
            };
            if (val != null)
                param.ParamType = val.GetType();
            return param;
        }

Then circulating the target value, the focus is below the value of the attribute to determine the type of object, usually with less because, record it

 private static string GetStrByType(NzSqlConditionInfo info)
        {
            switch (info.ParamType.FullName)
            {
                case "System.DateTime":
                    return GetDateByCal(info);
                case "System.String":
                    return  $" and { info.ParamName}{ info.ParamCal}'{Utils.Filter((string) info.ParamValue)}'";
                default:
                    return $" and { info.ParamName}{ info.ParamCal}{Utils.ObjectToStr(info.ParamValue)}";
            }
        }

A method of splicing a simple type of time sql

 private static string GetDateByCal(NzSqlConditionInfo info)
        {
            DateTime dt = Convert.ToDateTime(info.ParamValue);
            string dtstr = dt.ToString("yyyy/MM/dd");
            switch (info.ParamCal)
            {
                case ">=":
                    return $" and { info.ParamName}{ info.ParamCal}'{dtstr} 0:00:00'";
                case "<=":
                    return $" and { info.ParamName}{ info.ParamCal}'{dtstr} 23:59:59'";
                case "<":
                    return $" and { info.ParamName}{ info.ParamCal}'{dtstr} 23:59:59'";
                case "month":
                    return $" and {info.ParamName} between \'{dt}\' and \'{dt.AddMonths(1)}\' ";
                default:
                    return $" and {info.ParamName} between \'{dt}\'and \'{dt.AddDays(1)}\' ";
            }
        }

 

Guess you like

Origin blog.csdn.net/ab31ab/article/details/90929381