Ejemplo n.º 1
0
        private object GenerateResponse(string response, Type responseType)
        {
            object             t      = Activator.CreateInstance(responseType);
            BaseResponse       dyt    = t as BaseResponse;
            BaseResponseShadow shadow = null;

            try
            {
                //如果报错生成一个Response
                if (response.Contains("\"IsError\""))
                {
                    try
                    {
                        //t = JsonHelper.Deserialize(response, responseType);
                        shadow = JsonHelper.Deserialize <BaseResponseShadow>(response);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(response, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                dyt.SetBody(response);
                dyt.SetValues(true, ex.Message, ex.StackTrace, 0);
                return(t);
            }
            //null意味着没有报错
            if (shadow != null && !shadow.IsError)
            {
                //将返回的对象值设置到response的第一个属性上面
                PropertyInfo[] responseProperties = responseType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                if (responseProperties != null && responseProperties.Length > 0)
                {
                    BaseResponseShadow o = null;
                    try
                    {
                        o = JsonHelper.Deserialize <BaseResponseShadow>(response);
                        object data = null;
                        if (o.Data is JArray && responseProperties[0].PropertyType.Equals(typeof(DataTable)))
                        {
                            data = (o.Data as JArray).ToDataTable();
                        }
                        else if (o.Data is JObject)
                        {
                            data = (o.Data as JObject).ToObject(responseProperties[0].PropertyType);
                        }
                        else if (o.Data is JArray)
                        {
                            data = (o.Data as JArray).ToListObject(responseProperties[0].PropertyType);
                        }
                        else
                        {
                            data = o.Data;
                        }
                        if (data is object)
                        {
                            try
                            {
                                data = Convert.ChangeType(data, responseProperties[0].PropertyType);
                            }
                            catch
                            {
                                data = data.Clone(responseProperties[0].PropertyType);
                            }
                        }
                        //t.TrySetValue(responseProperties[0].Name, data);
                        responseProperties[0].SetValue(t, data, null);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(response, ex);
                    }
                }
            }
            else
            {
                //否则设置错误信息
                dyt.SetValues(shadow.IsError, shadow?.ErrorMessage, shadow?.StackTrace, shadow.Code);
            }
            dyt.SetBody(response);
            return(t);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="response"></param>
        /// <param name="responseType"></param>
        /// <param name="toProperty"></param>
        /// <returns></returns>
        protected object GenerateResponse(string response, Type responseType)
        {
            object             t            = Activator.CreateInstance(responseType);
            DefaultResponse    dyt          = t as DefaultResponse;
            BaseResponse       baseResponse = t as BaseResponse;
            BaseResponseShadow shadow       = null;

            //如果报错生成一个Response
            if (response.Contains("\"IsError\""))
            {
                try
                {
                    //t = JsonHelper.Deserialize(response, responseType);
                    shadow = JsonHelper.Deserialize <BaseResponseShadow>(response);
                    //否则设置错误信息
                    if (dyt != null)
                    {
                        dyt.SetValues(shadow.IsError, shadow.Code, shadow.Message, shadow.Exception);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(response, ex);
                }
            }
            else
            {
                PropertyInfo[] responseProperties = responseType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                if (responseProperties != null && responseProperties.Length > 0)
                {
                    PropertyInfo result = responseProperties[0];
                    //先查找有没有sdk result attribute的属性
                    foreach (var p in responseProperties)
                    {
                        var pro = p.GetCustomAttributes(typeof(SDKResultAttribute), true);
                        if (pro != null && pro.Length > 0)
                        {
                            result = p;
                            break;
                        }
                    }
                    //将返回的对象值设置到response的第一个属性上面
                    BaseResponseShadow o = null;
                    try
                    {
                        o = new BaseResponseShadow();
                        object data = JsonHelper.Deserialize(response, result.PropertyType);
                        if (data is object)
                        {
                            try
                            {
                                data = Convert.ChangeType(data, result.PropertyType);
                            }
                            catch
                            {
                                data = data.Clone(result.PropertyType);
                            }
                        }
                        //t.TrySetValue(responseProperties[0].Name, data);
                        result.SetValue(t, data, null);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(response, ex);
                    }
                }
                else
                {
                    t = JsonHelper.Deserialize(response, responseType);
                }
            }
            baseResponse.SetBody(response);
            return(t);
        }