Example #1
0
        public override bool LoadData(Type _mainClass, string _tableContent)
        {
            //> 检查数据类型的有效性
            if (null == _mainClass || false == _mainClass.IsClass || _mainClass.IsAbstract)
            {
                Output.Error("IDB:尝试载入无效的数据结构!");
                return(false);
            }

            //> 检查填充内容的有效性
            if (null == _tableContent || _tableContent.Length == 0)
            {
                Output.Error("IDB:数据载入{0}失败!请确保载入数据有效!", _mainClass.Name);
                return(false);
            }

            //> 检查有效的存储字段
            List <DBProperty> propertys = new List <DBProperty>();

            if (MakeValidPropertys(ref propertys, _mainClass))
            {
                Output.Error("IDB:载入失败!请确保{0}类中存在有效的存储字段!", _mainClass.Name);
                return(false);
            }

            string[]     lines         = _tableContent.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            string[]     tabPropertys  = lines[0].Split(new char[] { '\t' });
            DBProperty[] validProperty = new DBProperty[tabPropertys.Length];

            var valid = false;

            for (int i = 0, max = tabPropertys.Length; i < max; i++)
            {
                foreach (var v in propertys)
                {
                    if (v.GetName() == tabPropertys[i])
                    {
                        valid            = true;
                        validProperty[i] = v;
                        break;
                    }
                }
            }

            if (false == valid)
            {
                Output.Error("IDB:载入失败!{0}类中没有发现与数据匹配的字段或变量!", _mainClass.Name);
                return(false);
            }

            //> 最后一行数据的索引值
            int endLength = lines.Length - 1;

            //> 写入行数去掉末尾空行(wps导出的转义符文件总是多出一个空行)
            int maxCount = string.IsNullOrEmpty(lines[endLength]) ? endLength - 1 : endLength;

            //> 用来分割的转义符
            char[] splits = new char[] { '\t' };

            //> 表类的数组类型
            Type tabArray = _mainClass.MakeArrayType();

            //> 创建一个临时的数组存储器,大小由maxCount指定
            object[] datas = (object[])System.Activator.CreateInstance(tabArray, maxCount);

            //> 实际填充的个数
            int itemCount = 0;

            //> 当前操作的数据实例
            object itemCur = null;

            //> 遍历写入所有内容
            for (int i = 1, max = lines.Length; i < max; i++)
            {
                string[] column = lines[i].Split(splits);

                //> 跳过不完整的数据行
                if (column.Length != tabPropertys.Length)
                {
                    continue;
                }

                //> 创建并向目标身上写入数据
                itemCur = System.Activator.CreateInstance(_mainClass);
                for (int n = 0, n_max = column.Length; n < n_max; n++)
                {
                    //> 跳过无效的属性字段
                    if (validProperty[n] != null)
                    {
                        validProperty[n].SetValue(itemCur, column[n]);
                    }
                }
                datas[itemCount++] = itemCur;
            }

            //> 矫正数组个数
            object[] final = datas;
            if (itemCount > 0)
            {
                if (itemCount != maxCount)
                {
                    final = (object[])System.Activator.CreateInstance(tabArray, itemCount);

                    Array.Copy(datas, final, final.Length);
                }
            }
            else
            {
                final = (object[])System.Activator.CreateInstance(tabArray, 0);
            }

            //> 创建该类型的列表对象
            //object arrayTable = System.Activator.CreateInstance(tabArray, (object)final);

            //> 检查有效的静态存储变量
            FieldInfo storageField = GetStorageField(_mainClass);

            if (null != storageField)
            {
                storageField.SetValue(null, final);
                Output.Log("IDB:已将数据写入 {0}[] {1};中!", _mainClass.Name, storageField.Name);
            }

            //> 将列表对象填充到对应实例中
            IProperty[] iPropertys = (IProperty[])(object)propertys.ToArray();

            //> 将数据存储图表
            Datas[_mainClass.Name] = new Data(new List <IProperty>(iPropertys), final, _mainClass);

            Output.Log("IDB:[{0}] 解析完成! 共计[{1}]行数据!", _mainClass.Name, final.Length);
            return(true);
        }
Example #2
0
        public override bool LoadData(string _content, string _key)
        {
            if (string.IsNullOrEmpty(_key))
            {
                Output.Error("IConfig:指定密钥无效!载入数据失败!");
                return(false);
            }

            if (_content == null || _content.Length == 0)
            {
                Output.Error("IConfig:数据载入 {0} 失败!请确保数据内容有效!", _key);
                return(false);
            }

            string[] lines = _content.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            string            temp;
            Object            currentObject  = null;
            List <DBProperty> validPropertys = new List <DBProperty>();
            List <Object>     validObject    = new List <Object>();

            for (int i = 0, max = lines.Length; i < max; i++)
            {
                temp = lines[i].Trim();

                //> 解析对象
                if (temp.Length > 2 && temp[0] == '[' && temp[temp.Length - 1] == ']')
                {
                    //> 清理上一个对象的数据
                    currentObject = null;
                    validPropertys.Clear();

                    //> 获得名称,这个名称应该与配置类型配对
                    temp = temp.Remove(0, 1);
                    temp = temp.Remove(temp.Length - 1, 1);

                    if (temp.Length == 0)
                    {
                        continue;
                    }

                    //> 查找对应的配置类型,如果没有,跳过
                    var type = ConfigTypes.Find((v) => { return(v.Name == temp); });

                    if (type == null)
                    {
                        continue;
                    }

                    //> 获取类的有效字段属性,如果没有有效的字段成员,跳过

                    if (MakeValidPropertys(ref validPropertys, type))
                    {
                        //> 新的对象的数据覆盖掉历史对象的数据,没有历史对象就创建新对象

                        currentObject = validObject.Find((v) => { return(v.GetType().Equals(type)); });

                        if (null == currentObject)
                        {
                            currentObject = Activator.CreateInstance(type);

                            //> 找到这个类设置的对应存储变量,将他填充进去
                            #region 将数据存入用户自定义的存储单位
                            var fieldStorage = GetStorageField(type, _key + type.Name);

                            if (fieldStorage != null)
                            {
                                fieldStorage.SetValue(null, currentObject);
                            }
                            #endregion

                            validObject.Add(currentObject);
                        }
                    }
                }
                else if (currentObject != null)
                {
                    var index = temp.IndexOf('=');

                    if (index == -1)
                    {
                        continue;
                    }

                    //> 获取'='号前面的值
                    var property = temp.Substring(0, index).Trim();

                    if (property.Length == 0)
                    {
                        continue;
                    }

                    DBProperty fieldInterface = validPropertys.Find((v) => { return(v.GetName() == property); });

                    if (null == fieldInterface)
                    {
                        continue;
                    }

                    //> 获取'='号尾部的值
                    var value = temp.Remove(0, index + 1).Trim();

                    if (value.Length == 0)
                    {
                        continue;
                    }

                    //> 为对象填充数据
                    fieldInterface.SetValue(currentObject, value);
                }
            }

            Datas[_key] = validObject;

            Output.Log("IConfig:{0}解析完毕!共计[{1}]个配置对象!", _key, validObject.Count);
            return(true);
        }