Esempio n. 1
0
        static void SetInstancePropertyValueByDicDic(Dictionary <string, Dictionary <string, string> > dicdic,
                                                     object instance, string belongParameterName, string propertyName)
        {
            //该属性为一个实体。
            var groupName = belongParameterName + "." + propertyName;

            if ("WhereExpression" == propertyName)
            {
                return;
            }
            var currentGroup = dicdic[groupName];
            var pps          = instance.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

            foreach (var p in pps)
            {
                if (p.PropertyType == typeof(string) || p.PropertyType.IsValueType)
                {
                    if (currentGroup.ContainsKey(p.Name))
                    {
                        var vv = TypeUtil.ConvertTo(currentGroup[p.Name], p.PropertyType);
                        MethodInvokeUtil.Invoke(instance, "set_" + p.Name, new object[] { vv });
                    }
                }
                else
                {
                    var pobj = Activator.CreateInstance(p.PropertyType, null);
                    p.SetValue(instance, pobj, null);

                    SetInstancePropertyValueByDicDic(dicdic, pobj, groupName, p.Name);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 通过变量实体属性和从dicdic中获取的方式,设置某个参数实体的值
        /// </summary>
        /// <param name="dicdic"></param>
        /// <param name="instance"></param>
        /// <param name="instanceType"></param>
        public static void SetEntityValueByDicDic(Dictionary <string, Dictionary <string, string> > dicdic,
                                                  object instance, ParameterInfo pinfo)
        {
            Type   instanceType  = pinfo.ParameterType;
            string parameterName = pinfo.Name;

            if (instanceType.IsClass)//必须为类
            {
                if (dicdic.Count == 0)
                {
                    return;
                }
                Dictionary <string, string> oneGroup = null;
                if (dicdic.ContainsKey(parameterName))//在对应的参数组里面查找
                {
                    oneGroup = dicdic[parameterName];
                }
                else
                {
                    oneGroup = dicdic[string.Empty];//散数据中查找
                }
                var instanceProperties = instanceType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                foreach (var p in instanceProperties)
                {
                    var propertyName = p.Name;
                    if (p.PropertyType == typeof(string) || p.PropertyType.IsValueType)
                    {
                        if (oneGroup.ContainsKey(propertyName))
                        {
                            var vv = TypeUtil.ConvertTo(oneGroup[propertyName], p.PropertyType);
                            MethodInvokeUtil.Invoke(instance, "set_" + p.Name, new object[] { vv });
                        }
                    }
                    else
                    {
                        if (p.GetValue(instance, null) == null)
                        {
                            var pobj = Activator.CreateInstance(p.PropertyType, null);
                            p.SetValue(instance, pobj, null);
                            SetInstancePropertyValueByDicDic(dicdic, pobj, parameterName, p.Name);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// moon所有的请求
        /// </summary>
        /// <param name="classFullName">类的完全限定名</param>
        /// <param name="methodName">方法名(只能是public)</param>
        /// <param name="context">当前的HttpContext</param>
        public static void Process(string classFullName, string methodName, HttpContext context)
        {
            var classType     = ControllerAssmeblyUtil.CreateType(classFullName);
            var classInstance = ControllerAssmeblyUtil.CreateInstance(classFullName);

            if (classInstance == null)
            {
                Moon.Orm.Util.LogUtil.Error("classInstance null,classFullName:" + classFullName + "  " + context.Request.RawUrl);
                var err = "current request doesn't exist";
                throw new Exception(err);
            }
            if (classType == null)
            {
                var err = "current request doesn't exist";
                throw new Exception(err);
            }

            var methodInfo = classType.GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

            if (methodInfo == null)
            {
                return;
            }
            //=================
            BaseController baseControllerInstance = null;
            Dictionary <string, object> viewData  = new Dictionary <string, object>();

            if (classInstance is BaseController)
            {
                BaseController obj = classInstance as BaseController;
                obj.CurrentHttpContext = context;
                viewData = obj.ViewData;
                baseControllerInstance = obj;
            }
            else
            {
                throw new Exception("请求的类没有继承BaseController");
            }
            var             atts       = methodInfo.GetCustomAttributes(typeof(ResultAttribute), true);
            ResultAttribute resultAttr = null;

            if (atts.Length > 0)
            {
                resultAttr = atts[0] as ResultAttribute;
            }
            else
            {
                resultAttr = new TemplateResultAttribute();//默认为模板类型
            }
            //------------
            object resultObj = null;
            //------------
            var customAspectAttributes = methodInfo.GetCustomAttributes(typeof(AspectAttribute), true);

            List <ValidationResult> resultList = new List <ValidationResult>();
            ErrorResultReturnType   fluentValidateResultType = ErrorResultReturnType.Json;
            bool dontValidateRequest = ExistAttribute <DontValidateRequestAttribute>(methodInfo);
            int  methodRequestType   = GetMethodSupportRequestType(methodInfo);

            if (context.Request.RequestType == "GET")
            {
                if (methodRequestType == (int)RequestType.POST)
                {
                    var err = "Current request is GET,but you didn't set a GET attribute to your method :" + classFullName + "->" + methodName;
                    Moon.Orm.Util.LogUtil.Error(err);
                    throw new Exception(err);
                }
            }
            else
            {
                if (methodRequestType == (int)RequestType.GET)
                {
                    var err = "Current request is POST,but you didn't set a POST attribute to your method :" + classFullName + "->" + methodName;
                    Moon.Orm.Util.LogUtil.Error(err);
                    throw new Exception(err);
                }
            }

            object[] parameters = ParameterUtil.GetParametersObject(dontValidateRequest, context.Request, methodInfo, ref resultList, ref fluentValidateResultType);
            //---------验证参数----------
            if (ValidateMode(resultList) == false)
            {
                baseControllerInstance.IsModelValidate = false;
                if (fluentValidateResultType == ErrorResultReturnType.Json)
                {
                    var json = Moon.Orm.Util.JsonUtil.ConvertObjectToJson(resultList);
                    context.Response.Write(json);
                    context.Response.Flush();
                    //context.Response.End();//2015年9月13日14:03:43
                    return;
                }
                else if (fluentValidateResultType == ErrorResultReturnType.PureData)
                {
                    baseControllerInstance.ValidationResults = resultList;
                }
            }
            else
            {
                baseControllerInstance.IsModelValidate = true;
            }
            //
            if (customAspectAttributes.Length > 0)
            {
                List <AspectAttribute> aspectAttributeList = new List <AspectAttribute>();
                foreach (var oneAspect in customAspectAttributes)
                {
                    aspectAttributeList.Add(oneAspect as AspectAttribute);
                }
                OrderByAspectAttributePriority(aspectAttributeList);
                foreach (var oneAspect in aspectAttributeList)
                {
                    var aspectResult = oneAspect.BeforeInvoke(methodInfo, context);
                    if (aspectResult == AspectResultType.Stop)
                    {
                        return;
                    }
                }
                resultObj = MethodInvokeUtil.Invoke(classInstance, methodName, parameters.Length == 0 ? null : parameters);
                foreach (var oneAspect in aspectAttributeList)
                {
                    var rr = oneAspect.AfterInvoke(methodInfo, context);
                    if (rr == AspectResultType.Stop)
                    {
                        return;
                    }
                }
            }
            else
            {
                resultObj = MethodInvokeUtil.Invoke(classInstance, methodName, parameters.Length == 0 ? null : parameters);
            }
            resultAttr.Response(context, classFullName, methodName, resultObj, viewData);
        }