Beispiel #1
0
        /// <summary>
        /// 读取
        /// </summary>
        /// <returns></returns>
        public object Read(string file)
        {
            if (!File.Exists(file))
            {
                return(Activator.CreateInstance(type));
            }

            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                string json = Encoding.UTF8.GetString(data);
                try
                {
                    return(I3JsonConvert.FromJson(json, type));
                }
                catch
                {
                    return(Activator.CreateInstance(type));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 转换为图片单元格,复制所有属性(目的是为了在ReportData经过json转换后,找回Cell中丢失的单元格类型信息,要求I3ReportImageCell代码中的属性,全部写在I3ReportCell中)
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public I3ReportImageCell ConvertToImageItem(int row, int col)
        {
            I3ReportCell old = GetCellItem(row, col);

            if (old != null && old is I3ReportImageCell)
            {
                return(old as I3ReportImageCell);
            }

            I3ReportImageCell item = null;

            if (old == null)
            {
                item = new I3ReportImageCell(row, col);
            }
            else
            {
                string json = I3JsonConvert.ToJson(old);
                item = (I3ReportImageCell)I3JsonConvert.FromJson(json, typeof(I3ReportImageCell));
            }
            this[row][col] = item;

            return(item);
        }
Beispiel #3
0
 /// <summary>
 /// 构建参数  构建失败会抛出异常
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 protected virtual object[] CreateParameters(HttpRequest request, MethodInfo method)
 {
     object[] ps = null;
     try
     {
         ParameterInfo[] parameters = method.GetParameters();
         if (parameters.Length > 0)
         {
             MethodParameterTypeAttribute[] attrs = method.GetCustomAttributes(typeof(MethodParameterTypeAttribute), true) as MethodParameterTypeAttribute[];
             ps = new object[parameters.Length];
             int i = 0;
             foreach (ParameterInfo parameter in parameters)//遍历方法的参数
             {
                 string val = request[parameter.Name];
                 val = HttpUtility.UrlDecode(val, Encoding.UTF8);
                 if (!string.IsNullOrEmpty(val))//参数值不能为空
                 {
                     //非最终类处理
                     MethodParameterTypeAttribute pta = null;
                     foreach (MethodParameterTypeAttribute attr in attrs)//查找此参数对应的MethodParameterTypeAttribute
                     {
                         if (attr.ParameterName == parameter.Name)
                         {
                             pta = attr;
                             break;
                         }
                     }
                     if (pta != null)
                     {
                         IMethodParameterTypeCreator provider = Activator.CreateInstance(pta.CreatorType) as IMethodParameterTypeCreator;
                         foreach (ParameterInfo p in parameters)//查找关联参数
                         {
                             if (p.Name == pta.RelParameterName)
                             {
                                 string relVal = request[pta.RelParameterName];
                                 //获取关联参数
                                 object data = string.IsNullOrEmpty(relVal) ? null : I3JsonConvert.FromJson(relVal, p.ParameterType);
                                 //根据本参数的值和关联参数对象 生成本参数的对象
                                 ps[i] = I3JsonConvert.FromJson(val, provider.GetParameterType(data));
                                 break;
                             }
                         }
                     }
                     else
                     {
                         if (parameter.ParameterType == typeof(string))
                         {
                             if (val.StartsWith("\"") && val.EndsWith("\""))
                             {
                                 //参数为string类型时,参数值两端可加双引号,也可不加(标准json是需要加的)
                                 try
                                 {
                                     ps[i] = I3JsonConvert.FromJson(val, parameter.ParameterType);
                                 }
                                 catch
                                 {
                                     ps[i] = val;
                                 }
                             }
                             else
                             {
                                 ps[i] = val;
                             }
                         }
                         else
                         {
                             if (parameter.ParameterType == typeof(bool))
                             {
                                 val = val == null ? null : val.ToLower();
                             }
                             ps[i] = I3JsonConvert.FromJson(val, parameter.ParameterType);
                         }
                     }
                 }
                 i++;
             }
         }
     }
     catch (Exception ex)
     {
         throw new JsonServiceException("参数获取失败!", ex);
     }
     return(ps);
 }