Beispiel #1
0
 /// <summary>
 /// 将字节流转换为指定对象
 /// </summary>
 /// <param name="bytes"> </param>
 /// <param name="formatProvider"> 它提供有关当前实例的格式信息 </param>
 /// <returns> </returns>
 public virtual T Deserialize <T>(byte[] bytes, IFormatProvider formatProvider)
 {
     try
     {
         var instance = Activator.CreateInstance <T>();
         var props    = PropertyHandlerCollection.Get(typeof(T));
         var kv       = Deserialize(bytes, formatProvider);
         if (kv == null)
         {
             throw new NotSupportedException("无法转为实体");
         }
         foreach (var o in kv)
         {
             props[o.Key]?.SetValue(instance, o.Value);
         }
         return(instance);
     }
     catch (Exception ex)
     {
         if (bytes?.Length > 0)
         {
             var charset = formatProvider?.GetFormat(typeof(Encoding)) as Encoding;
             if (charset == null)
             {
                 ex.Data["ResponseBody"] = ex.Source = "base64:" + Convert.ToBase64String(bytes);
             }
             else
             {
                 ex.Data["ResponseBody"] = ex.Source = charset.GetString(bytes);
             }
         }
         throw;
     }
 }
        /// <summary>
        /// 追加实体对象到 Http 参数
        /// </summary>
        /// <param name="param"> 参数将被追加到的参数集合 </param>
        /// <param name="model"> 需要追加到 Http 参数的实体对象 </param>
        public static void AddModel(this HttpParamsBase <object> param, object model)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (model == null)
            {
                return;
            }
            var props = PropertyHandlerCollection.Get(model.GetType());

            for (int i = 0, length = props.Count; i < length; i++)
            {
                var p = props[i];
                param[p.Name] = p.GetValue(model);
            }
        }
        /// <summary>
        /// 将有一个参数写入<paramref name="buffer" />中
        /// </summary>
        /// <param name="buffer"> 字符缓存 </param>
        /// <param name="preName"> 参数名 </param>
        /// <param name="value"> 参数值 </param>
        private void AppendObject(StringBuilder buffer, string preName, object value)
        {
            var str = TryToString(value);

            if ((str != null) || (value == null))
            {
                if (preName != null)
                {
                    AppendEscape(buffer, preName);
                    buffer.Append('=');
                }
                AppendEscape(buffer, str);
                return;
            }

            var array = (value as IEnumerable)?.GetEnumerator() ?? value as IEnumerator;

            if (array != null)
            {
                switch (ArrayEncodeMode)
                {
                case ArrayEncodeMode.Merge:
                    AppendEscape(buffer, preName);
                    buffer.Append('=');
                    if (array.MoveNext())
                    {
                        var v = array.Current;
                        AppendEscape(buffer, TryToString(v) ?? IOC.ComponentServices.ToJsonString(v));
                        while (array.MoveNext())
                        {
                            v = array.Current;
                            buffer.Append(',');
                            AppendEscape(buffer, TryToString(v) ?? IOC.ComponentServices.ToJsonString(v));
                        }
                    }
                    break;

                case ArrayEncodeMode.JQuery:
                    if (array.MoveNext())
                    {
                        AppendObject(buffer, preName + "[]", array.Current);
                        while (array.MoveNext())
                        {
                            buffer.Append('&');
                            AppendObject(buffer, preName + "[]", array.Current);
                        }
                    }
                    break;

                case ArrayEncodeMode.Json:
                    AppendObject(buffer, preName, IOC.ComponentServices.ToJsonString(array));
                    break;

                case ArrayEncodeMode.Asp:
                    if (array.MoveNext())
                    {
                        var i = 0;
                        AppendObject(buffer, $"{preName}[{i}]", array.Current);
                        while (array.MoveNext())
                        {
                            i++;
                            buffer.Append('&');
                            AppendObject(buffer, $"{preName}[{i}]", array.Current);
                        }
                    }
                    break;

                case ArrayEncodeMode.Default:
                default:
                    if (array.MoveNext())
                    {
                        AppendObject(buffer, preName, array.Current);
                        while (array.MoveNext())
                        {
                            buffer.Append('&');
                            AppendObject(buffer, preName, array.Current);
                        }
                    }
                    break;
                }
                return;
            }

            var props  = PropertyHandlerCollection.Get(value.GetType());
            var pCount = props.Count;

            if (pCount == 0)
            {
                if (preName != null)
                {
                    AppendEscape(buffer, preName);
                    buffer.Append('=');
                }
                AppendEscape(buffer, value.ToString());
                return;
            }

            switch (ObjectEncodeMode)
            {
            case ObjectEncodeMode.NameOnly:
            {
                AppendObject(buffer, props[0].Name, props[0].GetValue(value));
                for (var i = 1; i < pCount; i++)
                {
                    buffer.Append('&');
                    AppendObject(buffer, props[i].Name, props[i].GetValue(value));
                }
            }
            break;

            case ObjectEncodeMode.JQuery:
            {
                AppendObject(buffer, ConcatName(preName, props[0].Name, 1), props[0].GetValue(value));
                for (var i = 1; i < pCount; i++)
                {
                    buffer.Append('&');
                    AppendObject(buffer, ConcatName(preName, props[i].Name, 1), props[i].GetValue(value));
                }
            }
            break;

            case ObjectEncodeMode.Json:
                AppendObject(buffer, preName, IOC.ComponentServices.ToJsonString(value));
                break;

            case ObjectEncodeMode.Default:
            default:
            {
                AppendObject(buffer, ConcatName(preName, props[0].Name, 0), props[0].GetValue(value));
                for (var i = 1; i < pCount; i++)
                {
                    buffer.Append('&');
                    AppendObject(buffer, ConcatName(preName, props[i].Name, 0), props[i].GetValue(value));
                }
            }
            break;
            }
        }