Ejemplo n.º 1
0
        /// <summary>
        /// 根据路径查询配置节点
        /// </summary>
        /// <param name="config">需要查询的配置的根节点</param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ConfigNode Select(this Config config, string path)
        {
            if (config == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(path))
            {
                return(ConfigNode.Invalid);
            }
            var index = new IndexerArgsParser(path);
            var node  = (ConfigNode)config;

            foreach (var i in index)
            {
                node = i.IsKey ? node[i.Key] : node[i.Index];
            }
            return(node);
        }
Ejemplo n.º 2
0
        public override ConfigNode this[string path]
        {
            get
            {
                if (string.IsNullOrEmpty(path))
                {
                    return(Invalid);
                }
                var index = new IndexerArgsParser(path);
                var ee    = index.GetEnumerator();
                if (ee.MoveNext())
                {
                    var i    = ee.Current;
                    var node = i.IsKey ? base[i.Key] : base[i.Index];
                    while (ee.MoveNext())
                    {
                        i    = ee.Current;
                        node = i.IsKey ? node[i.Key] : node[i.Index];
                    }
                    return(node);
                }
                return(Invalid);
            }

            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }

                if (string.IsNullOrEmpty(path))
                {
                    throw new ArgumentException(nameof(path), "路径无效");
                }
                var index = new IndexerArgsParser(path);
                var ee    = index.GetEnumerator();
                if (ee.MoveNext())
                {
                    var i    = ee.Current;
                    var node = i.IsKey ? base[i.Key] : base[i.Index];
                    if (ee.MoveNext() == false)
                    {
                        i = ee.Current;
                        if (i.IsKey)
                        {
                            base[i.Key] = value;
                        }
                        else
                        {
                            base[i.Index] = value;
                        }
                        return;
                    }

                    do
                    {
                        i    = ee.Current;
                        node = i.IsKey ? node[i.Key] : node[i.Index];
                    } while (ee.MoveNext());

                    if (i.IsKey)
                    {
                        node.Parent[i.Key] = value;
                    }
                    else
                    {
                        node.Parent[i.Index] = value;
                    }
                    return;
                }
                throw new ArgumentException(nameof(path), "路径无效");
            }
        }