Exemplo n.º 1
0
        public static void LoadNodes(object dataObj, int depth, string dataStr)
        {
            var             dataType  = dataObj.GetType();
            List <SaveNode> saveNodes = new List <SaveNode>();

            List <FieldInfo> fieldList = new List <FieldInfo>();
            //if (fieldNames == null || fieldNames.Count == 0)
            {
                var fields = dataType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                fieldList.AddRange(fields);
            }


            Dictionary <int, FieldInfo> loadFields   = new Dictionary <int, FieldInfo>();
            List <FieldInfo>            sortedFields = new List <FieldInfo>();

            foreach (var fieldInfo in fieldList)
            {
                SaveField[] attributes = (SaveField[])fieldInfo.GetCustomAttributes(typeof(SaveField), false);
                if (attributes != null && attributes.Length > 0)
                {
                    loadFields.Add(attributes[0]._SaveIDX - 1, fieldInfo);
                }
            }
            for (int i = 0; i < loadFields.Count; ++i)
            {
                sortedFields.Add(loadFields[i]);
            }

            string[] valueStrs = dataStr.Split(SaveSplitChars[depth]);
            if (valueStrs.Length != sortedFields.Count)
            {
                return;
            }

            for (int i = 0; i < sortedFields.Count; ++i)
            {
                if (IsBaseType(sortedFields[i].FieldType))
                {
                    SetBaseField(dataObj, sortedFields[i], valueStrs[i]);
                }
                else if (sortedFields[i].FieldType.Name.Contains("List"))
                {
                    string typeStr   = sortedFields[i].FieldType.ToString();
                    var    valueList = Activator.CreateInstance(sortedFields[i].FieldType) as IList;

                    int           idx1          = typeStr.IndexOf('[');
                    int           idx2          = typeStr.IndexOf(']');
                    string        childTypeName = typeStr.Substring(idx1 + 1, idx2 - idx1 - 1);
                    string[]      listValues    = valueStrs[i].Split(SaveSplitChars[depth + 1]);
                    List <string> notEmptyList  = new List <string>(listValues);
                    ListTool.ExcludeEmptyStr(notEmptyList);

                    Type childType = null;
                    for (int j = 0; j < notEmptyList.Count; ++j)
                    {
                        if (childType == null)
                        {
                            childType = Type.GetType(childTypeName);
                        }
                        if (IsBaseType(childType))
                        {
                            var hash = GetBaseFieldValue(childType, notEmptyList[j]);
                            valueList.Add(hash[childType.Name.ToString()]);
                        }
                        else
                        {
                            var listItem = Activator.CreateInstance(Type.GetType(childTypeName));
                            LoadNodes(listItem, depth + 2, notEmptyList[j]);
                            valueList.Add(listItem);
                        }
                    }

                    sortedFields[i].SetValue(dataObj, valueList);
                }
                else if (sortedFields[i].FieldType.IsEnum)
                {
                    sortedFields[i].SetValue(dataObj, int.Parse(valueStrs[i]));
                }
                else if (sortedFields[i].FieldType.BaseType == typeof(Tables.TableRecordBase))
                {
                    string recordID = valueStrs[i];

                    var record = TableReadEx.GetTableRecord(sortedFields[i].FieldType.Name.Replace("Record", ""), recordID);
                    if (record == null)
                    {
                        Debug.LogError("Load Record error:" + sortedFields[i].FieldType.Name + "," + recordID);
                    }
                    else
                    {
                        sortedFields[i].SetValue(dataObj, record);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(valueStrs[i]))
                    {
                        var constumValue = Activator.CreateInstance(sortedFields[i].FieldType);
                        LoadNodes(constumValue, depth + 1, valueStrs[i]);
                        sortedFields[i].SetValue(dataObj, constumValue);
                    }
                }
            }

            var iniMethod = dataType.GetMethod("InitFromLoad");

            if (iniMethod != null)
            {
                iniMethod.Invoke(dataObj, new object[] { });
            }
        }