Exemplo n.º 1
0
    private void OnLoadTextCallback(string path, byte[] bytes)
    {
        if (!mLoadHandles.ContainsKey(path))
        {
            //出现严重错误
            return;
        }
        string text = "";

        LoadHandler handle = mLoadHandles[path] as LoadHandler;

        if (handle.isXML)
        {
            text = System.Text.Encoding.UTF8.GetString(bytes);
        }
        else
        {
            text = System.Text.Encoding.Unicode.GetString(bytes);
        }

        if (string.IsNullOrEmpty(text))
        {
            return;
        }

        //Hashtable tb = new Hashtable(new CustomEqualityComparer());

        DataTable tb = new DataTable();

        if (!handle.isXML)
        {
            WDBData dbData = null;
            try
            {
                dbData = TextAnalyze.Analyze(text);
            }
            catch (Exception analyzeException)
            {
                throw new Exception("加载" + path + "失败: " + analyzeException.Message);
            }

            for (int i = 0; i < dbData.GetRecordCount(); ++i)
            {
                WDBSheetLine line = dbData.GetDataByNumber(i);

                if (line != null)
                {
                    object item = System.Activator.CreateInstance(handle.type);
                    try
                    {
                        tb.Add(loadLine(line, item, handle.keyIdx), item);
                    }
                    catch (Exception exp)
                    {
                        throw new Exception("解析" + path + "失败: " + exp.Message);
                    }
                }
            }
        }
        else
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(text);
            XmlNode node = xDoc.FirstChild;
            node = node.NextSibling;
            XmlNodeList nodeList = node.ChildNodes;
            for (int i = 0; i < nodeList.Count; ++i)
            {
                XmlNode childNode = nodeList[i];

                if (!string.IsNullOrEmpty(childNode.Name) &&
                    childNode.LocalName == "#comment")
                {
                    continue;
                }
                object item = System.Activator.CreateInstance(handle.type);

                Type itemType = item.GetType();
                System.Reflection.FieldInfo[] fields = itemType.GetFields();

                foreach (System.Reflection.FieldInfo f in fields)
                {
                    XmlNode nd = childNode.Attributes.GetNamedItem(f.Name);
                    if (nd != null)
                    {
                        if (f.FieldType.Name == "Int32")
                        {
                            f.SetValue(item, System.Convert.ToInt32(nd.Value));
                        }
                        if (f.FieldType.Name == "Single")
                        {
                            f.SetValue(item, System.Convert.ToSingle(nd.Value));
                        }
                        if (f.FieldType.Name == "String")
                        {
                            f.SetValue(item, nd.Value);
                        }
                    }
                }

                tb.Add(fields[0].GetValue(item), item);
            }
        }

        mLoadHandles.Remove(path);
        mTableList.Add((int)handle.dataType, tb);


        if (mLoadHandles.Count <= 0)
        {
            OnAllLoad();
        }
    }