Ejemplo n.º 1
0
        /// <summary>
        /// T要转化到的类型
        /// 如TestConfig.asset每行的数据要转化为TestConfig.cs里面的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T CreateRowInstance <T>(string id) where T : class
        {
            if (this.idList.Count != this.valueList.Count)             //数量不等,报错
            {
                LogCat.LogErrorFormat("AssetDataSource OnAfterDeserialize Failed! keys.Count:{0}, values.Count:{1}",
                                      idList.Count, this.valueList.Count);
                return(default(T));
            }

            if (this.dataSourceItemListDict == null)
            {
                this.dataSourceItemListDict = new Dictionary <string, ExcelRow>();
                for (int i = 0; i < this.idList.Count; i++)
                {
                    this.dataSourceItemListDict[this.idList[i]] = this.valueList[i];
                }
            }

            ExcelRow excelRow = null;
            T        result   = default(T);    //最终的数据

            if (this.dataSourceItemListDict.TryGetValue(id, out excelRow))
            {
                result = Activator.CreateInstance(typeof(T), true) as T;
                Dictionary <string, MemberAccessor> accessorDict =
                    MemberAccessorPool.instance.GetAccessors(typeof(T), BindingFlagsConst.Instance);
                for (int j = 0; j < this.headerList.Count; j++)                 //循环每一列
                {
                    ExcelHeader    excelHeader    = this.headerList[j];
                    object         value          = ExcelDatabaseUtil.Convert(excelRow.valueList[j].value, excelHeader.type);    //转化对对应列所对应的类型的数据
                    MemberAccessor memberAccessor = null;
                    if (accessorDict.TryGetValue(excelHeader.name, out memberAccessor))
                    {
                        try
                        {
                            memberAccessor.SetValue(result, value);                             //将值设置到result中
                        }
                        catch
                        {
                            LogCat.LogErrorFormat("The value \"{0}\" is {1} in config. Please check the type you defined. ",
                                                  excelHeader.name, excelHeader.type);
                        }
                    }
                }

                return(result);
            }

            return(default(T));
        }
Ejemplo n.º 2
0
        public static List <ExcelDataType> GetSheetHeaderTypes(ISheet sheet, int headerTypeRowNum)
        {
            var list = new List <ExcelDataType>();
            var row  = sheet.GetRow(headerTypeRowNum);

            for (var i = 0; i < (int)row.LastCellNum; i++)
            {
                var cell = row.GetCell(i);
                if (cell == null || GetCellValue(cell).Trim().IsNullOrWhiteSpace())
                {
                    continue;
                }

                var item = ExcelDatabaseUtil.String2DataType(GetCellValue(cell).Trim());
                list.Add(item);
            }

            return(list);
        }