Example #1
0
        /// <summary>
        /// Recursive search that returns a list of all nodes with the specified name
        /// </summary>
        /// <param name="name">The node name.</param>
        /// <param name="searchAllChildren">if set to <c>true</c> search deeply.</param>
        /// <param name="collection">The collection under inspection.</param>
        /// <param name="foundNodes">The previously found nodes.</param>
        /// <returns>A collection of the previously found nodes with any newly found nodes from the collection under inspection.</returns>
        private SettingsNodeList FindInternal(string name, bool searchAllChildren, SettingsNodeList collection, SettingsNodeList foundNodes)
        {
            if ((collection == null) || (foundNodes == null))
            {
                return(null);
            }

            for (int i = 0; i < collection.Count; i++)
            {
                if (collection[i] != null && collection[i].Name == name)
                {
                    foundNodes.Add(collection[i]);
                }
            }

            if (searchAllChildren)
            {
                for (int i = 0; i < collection.Count; i++)
                {
                    if (collection[i] != null && collection[i].Nodes != null && collection[i].Nodes.Count > 0)
                    {
                        foundNodes = this.FindInternal(name, searchAllChildren, collection[i].Nodes, foundNodes);
                    }
                }
            }

            return(foundNodes);
        }
Example #2
0
        /// <summary>
        /// Returns a list of all nodes in the collection that have names starting with the specified string.
        /// </summary>
        /// <param name="nameBeginning">The name beginning.</param>
        /// <returns>A SettingsNodeList of found nodes.</returns>
        public SettingsNodeList SelectNodesStartingWith(string nameBeginning)
        {
            SettingsNodeList nodes = new SettingsNodeList(_parent);

            foreach (SettingsNode node in _nodeIndices)
            {
                if (node.Name.StartsWith(nameBeginning))
                {
                    nodes.Add(node);
                }
            }

            return(nodes);
        }
Example #3
0
        /// <summary>
        /// Returns a list of all nodes in the collection that have names which contain the specified string.
        /// </summary>
        /// <param name="namePartial">The partial node name.</param>
        /// <returns>A SettingsNodeList of found nodes.</returns>
        public SettingsNodeList SelectNodesContaining(string namePartial)
        {
            SettingsNodeList nodes = new SettingsNodeList(_parent);

            foreach (SettingsNode node in _nodeIndices)
            {
                if (node.Name.Contains(namePartial))
                {
                    nodes.Add(node);
                }
            }

            return(nodes);
        }
        /// <summary>
        /// Used internally to deeply load from the SettingsDocument.
        /// </summary>
        private static void SetValues(object parent, PropertyInfo[] properties, SettingsNodeList docNodes)
        {
            if (properties == null)
            {
                return;
            }

            if (parent == null)
            {
                return;
            }

            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i] == null)
                {
                    continue;
                }

                if (!docNodes.ContainsName(properties[i].Name))
                {
                    continue;
                }

                SettingsNode node = docNodes.GetNode(properties[i].Name);

                ParameterInfo[] parInfo = properties[i].GetIndexParameters(); //if represents a array
                if (parInfo != null && parInfo.Length > 0)
                {
                    continue;
                }

                Type propertyType = properties[i].PropertyType;

                if (propertyType.IsPrimitive || propertyType.IsEnum || propertyType == typeof(string))
                {
                    if (!node.HasAValue || !properties[i].CanWrite)
                    {
                        continue;
                    }

                    try
                    {
                        object value = ConvertValue(node.Value, propertyType);
                        properties[i].SetValue(parent, value, null);
                        continue;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Failed to load setting: " + node.FullPath, ex);
                    }
                }

                if (propertyType.GetInterface("ISettings", true) != null)
                {
                    ISettings settingsObject = properties[i].GetValue(parent, null) as ISettings;

                    if (settingsObject == null)
                    {
                        settingsObject = Activator.CreateInstance(propertyType) as ISettings;
                    }

                    settingsObject.UpdateFromSettingsNode(node);
                    continue;
                }

                if (IsListType(propertyType))
                {
                    IList list = properties[i].GetValue(parent, null) as IList;

                    if (list is Array)
                    {
                        Type elementType = list.GetType().GetElementType();

                        if (list == null || list.Count == 0)
                        {
                            list = Array.CreateInstance(elementType, node.Nodes.Count);
                        }

                        for (int j = 0; j < Math.Min(list.Count, node.ListLength); j++)
                        {
                            if (list[j] == null)
                            {
                                list[j] = CreateObjectFromNode(node[j], elementType);
                            }
                            else
                            {
                                UpdateObjectFromNode(list[j], node[j]);
                            }
                        }

                        properties[i].SetValue(parent, list, null);
                    }
                    else
                    {
                        Type listType = list.GetType();

                        if (!listType.IsGenericType)
                        {
                            if (!listType.BaseType.IsGenericType)
                            {
                                continue;
                            }

                            listType = listType.BaseType;
                        }

                        Type[] typeArguments = listType.GetGenericArguments();

                        if (typeArguments.Length < 1)
                        {
                            continue;
                        }

                        Type elementType = typeArguments[0];

                        if (list == null)
                        {
                            list = Activator.CreateInstance(list.GetType()) as IList;
                        }

                        list.Clear();

                        foreach (SettingsNode subNode in node.Nodes)
                        {
                            object item = null;

                            if (elementType.IsPrimitive || elementType.IsEnum || elementType == typeof(string))
                            {
                                item = ConvertValue(subNode.Value, elementType);
                            }
                            else
                            {
                                item = CreateObjectFromNode(subNode, elementType);
                            }

                            list.Add(item);
                        }

                        properties[i].SetValue(parent, list, null);
                    }

                    continue;
                }

                PropertyInfo[] propertyInfos = properties[i].PropertyType.GetProperties(BindingFlags.Public |
                                                                                        BindingFlags.Instance);

                object child = properties[i].GetValue(parent, null);

                if (child == null && node != null)
                {
                    Type childType = null;

                    try
                    {
                        childType = properties[i].PropertyType;
                        child     = Activator.CreateInstance(childType);
                        properties[i].SetValue(parent, child, null);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format("Failed to load setting: {0}. Could not create new {1}. ", node.FullPath, childType), ex);
                    }
                }

                SetValues(child, propertyInfos, node.Nodes);
            }
        }