Example #1
0
        private void _saveToXml(object subData, string rootPath)
        {
            Type type = subData.GetType();

            var arrFields = type.GetMembers(BindingFlags.Instance | BindingFlags.Public);
            var arrProps  = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            MemberInfo[] arr = arrFields.Cast <MemberInfo>().Concat(arrProps).ToArray();

            //string rootPath = "";
            //if(subData == data && type.IsDefined(typeof(XmlRoot))) {
            //	//var attr = type.GetCustomAttribute<XmlPath>();
            //	rootPath = type.GetCustomAttribute<XmlRoot>().path;
            //	//rootPath = path;
            //}
            if (type.IsDefined(typeof(XmlRoot)))
            {
                rootPath = formatPath(rootPath, type.GetCustomAttribute <XmlRoot>().path);
            }

            foreach (var mi in arr)
            {
                //string name = pi.Name;
                object val = getMemberValue(mi, subData);
                //object val = pi.GetValue(subData);
                //Debug.WriteLine("bb:" + name + "," + val);

                if (mi.IsDefined(typeof(XmlAttr), false))
                {
                    //attr
                    string path = formatPath(rootPath, mi.GetCustomAttribute <XmlAttr>().path);
                    _xml.setAttr(path, val.ToString());
                }
                else if (mi.IsDefined(typeof(XmlValue), false))
                {
                    //value
                    string path = formatPath(rootPath, mi.GetCustomAttribute <XmlValue>().path);
                    _xml.setValue(path, val.ToString());
                }
                else if (mi.IsDefined(typeof(XmlChild), false))
                {
                    //child
                    if (val != null)
                    {
                        string path = formatPath(rootPath, mi.GetCustomAttribute <XmlChild>().path);
                        _saveToXml(val, path);
                    }
                }
                else if (mi.IsDefined(typeof(XmlListAttr), false))
                {
                    //list attr
                    string path = formatPath(rootPath, mi.GetCustomAttribute <XmlListAttr>().path);

                    if (val != null && val is IEnumerable)
                    {
                        int    idx          = path.LastIndexOf(".");
                        string rootPathTemp = "";
                        string subPath      = path;
                        if (idx >= 0)
                        {
                            rootPathTemp = path.Substring(0, idx);
                            subPath      = path.Substring(idx + 1);
                        }

                        int i = 0;
                        foreach (var item in val as IEnumerable)
                        {
                            string pathTemp = rootPathTemp + $"[{i}].{subPath}";
                            _xml.setAttr(pathTemp, item.ToString());
                            ++i;
                        }
                        //remove out of range
                        List <XmlCtl> lst = _xml.children(rootPathTemp);
                        if (i < lst.Count)
                        {
                            for (int j = i; j < lst.Count; ++j)
                            {
                                _xml.removeInTarget(rootPathTemp, i);
                            }
                        }
                    }
                }
                else if (mi.IsDefined(typeof(XmlListValue), false))
                {
                    //list value
                    string path = formatPath(rootPath, mi.GetCustomAttribute <XmlListValue>().path);

                    if (val != null && val is IEnumerable)
                    {
                        int i = 0;
                        foreach (var item in val as IEnumerable)
                        {
                            string pathTemp = path + $"[{i}]";
                            _xml.setValue(pathTemp, item.ToString());
                            ++i;
                        }
                        //remove out of range
                        List <XmlCtl> lst = _xml.children(path);
                        if (i < lst.Count)
                        {
                            for (int j = i; j < lst.Count; ++j)
                            {
                                _xml.removeInTarget(path, i);
                            }
                        }
                    }
                }
                else if (mi.IsDefined(typeof(XmlListChild), false))
                {
                    //list child
                    string path = formatPath(rootPath, mi.GetCustomAttribute <XmlListChild>().path);

                    if (val != null && val is IEnumerable)
                    {
                        int i = 0;
                        foreach (var item in val as IEnumerable)
                        {
                            string pathTemp = path + $"[{i}]";
                            _saveToXml(item, pathTemp);
                            ++i;
                        }
                        //remove out of range
                        List <XmlCtl> lst = _xml.children(path);
                        if (i < lst.Count)
                        {
                            for (int j = i; j < lst.Count; ++j)
                            {
                                _xml.removeInTarget(path, i);
                            }
                        }
                    }
                }
                else if (mi.IsDefined(typeof(XmlMap), false))
                {
                    //map
                    XmlMap mapInfo = mi.GetCustomAttribute <XmlMap>();

                    string path = formatPath(rootPath, mapInfo.rootPath);

                    IDictionary objVal = (val as IDictionary);
                    if (objVal != null)
                    {
                        int i = 0;
                        foreach (var key in objVal.Keys)
                        {
                            string keyPath = formatPath(path + $"[{i}]", mapInfo.keyPath);
                            switch (mapInfo.keyType)
                            {
                            case XmlKeyType.Attr: _xml.setAttr(keyPath, key.ToString()); break;

                            case XmlKeyType.Value: _xml.setValue(keyPath, key.ToString()); break;
                            }

                            string valPath = formatPath(path + $"[{i}]", mapInfo.valuePath);
                            switch (mapInfo.valueType)
                            {
                            case XmlValueType.Attr: _xml.setAttr(valPath, objVal[key].ToString()); break;

                            case XmlValueType.Value: _xml.setValue(valPath, objVal[key].ToString()); break;

                            case XmlValueType.Child: {
                                _saveToXml(objVal[key], valPath);
                                break;
                            }
                            }
                            ++i;
                        }
                        //remove out of range
                        List <XmlCtl> lst = _xml.children(path);
                        if (i < lst.Count)
                        {
                            for (int j = i; j < lst.Count; ++j)
                            {
                                _xml.removeInTarget(path, i);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        private void setMap(object data, MemberInfo mi, string rootPath)
        {
            XmlMap xmlAttr = mi.GetCustomAttribute <XmlMap>();

            object val = getMemberValue(mi, data);

            Type type      = getMemberType(mi);
            Type keyType   = null;
            Type valueType = null;

            if (type.IsGenericType)
            {
                //泛型
                Type[] arrType = type.GetGenericArguments();
                keyType   = arrType[0];
                valueType = arrType[1];
            }
            else
            {
                var mtTemp = type.GetMethod("Add");
                keyType   = mtTemp.GetParameters()[0].ParameterType;
                valueType = mtTemp.GetParameters()[1].ParameterType;
            }

            //IDictionary<T>
            object map = val;

            if (map == null)
            {
                map = Activator.CreateInstance(type);
                setMemberValue(mi, data, map);
            }

            //clear
            var mtChear = type.GetMethod("Clear");

            mtChear.Invoke(map, new object[0]);

            var    mtAdd = type.GetMethod("Add", new Type[] { keyType, valueType });
            string path  = formatPath(rootPath, xmlAttr.rootPath);

            List <XmlCtl> lstChild = _xml.children(formatPath(rootPath, xmlAttr.rootPath));

            for (int i = 0; i < lstChild.Count; ++i)
            {
                //key
                object objKey  = null;
                string keyPath = formatPath(path + $"[{i}]", xmlAttr.keyPath);
                switch (xmlAttr.keyType)
                {
                case XmlKeyType.Attr: objKey = getData(xml.attr, keyPath, keyType); break;

                case XmlKeyType.Value: objKey = getData(xml.value, keyPath, keyType); break;
                }

                //value
                object objValue  = null;
                string valuePath = formatPath(path + $"[{i}]", xmlAttr.valuePath);
                switch (xmlAttr.valueType)
                {
                case XmlValueType.Attr: objValue = getData(xml.attr, valuePath, valueType); break;

                case XmlValueType.Value: objValue = getData(xml.value, valuePath, valueType); break;

                case XmlValueType.Child: {
                    objValue = Activator.CreateInstance(valueType);
                    _load(objValue, valuePath);
                    break;
                }
                }

                mtAdd.Invoke(map, new object[] { objKey, objValue });
            }
        }