예제 #1
0
        public SettingsNode ConvertToSettingsNode()
        {
            SettingsNode node = new SettingsNode("Hga");

            node.AddChild("Index", "", typeof(int), Index);
            node.AddChild("Hga_Status", "", typeof(HGAStatus), Hga_Status);

            return(node);
        }
        private static void UpdateNodeFromNode(SettingsNode sourceNode, SettingsNode destinationNode)
        {
            if (sourceNode.IsList)
            {
                destinationNode.RemoveAllChildren();
                destinationNode.ListLength = sourceNode.ListLength;
            }

            foreach (SettingsNode node in sourceNode.Nodes)
            {
                if (destinationNode.HasAValue)
                {
                    destinationNode.RemoveAllChildren();
                }

                if (destinationNode.Nodes.ContainsName(node.Name))
                {
                    UpdateNodeFromNode(node, destinationNode.Nodes[node.Name]);
                }
                else
                {
                    destinationNode.AddChild(node.Clone());
                }
            }
        }
        public void GetNode_PathDiffCasing_ReturnsNode()
        {
            SettingsNode node = new SettingsNode("Test");

            node.AddChild("Test Child").AddChild("Child of Child");

            SettingsNode foundNode = node.GetNode("TEST CHILD | ChILd oF chIld");

            Assert.IsNotNull(foundNode);
        }
        public void GetNode_PathExists_ReturnsNode()
        {
            SettingsNode node = new SettingsNode("Test");

            node.AddChild("Test Child").AddChild("Child of Child");

            SettingsNode foundNode = node.GetNode("Test Child | Child of Child");

            Assert.IsNotNull(foundNode);
        }
        public void GetLevel_ChildReturnsOneMoreThanParent()
        {
            SettingsNode node      = new SettingsNode("Test");
            SettingsNode childNode = new SettingsNode("Child");

            node.AddChild(childNode);

            int parentLevel = node.Level;
            int childLevel  = childNode.Level;

            Assert.AreEqual(parentLevel + 1, childLevel);
        }
        /// <summary>
        /// Used internally to deeply convert the specified object and attach to the specified node.
        /// </summary>
        private static void GetNodes(object obj, Type type, SettingsNode node)
        {
            if (obj == null)
            {
                node.AddChild("");
                return;
            }

            if (type.IsPrimitive || type.IsEnum || type == typeof(string))
            {
                node.AddChild(obj.ToString());
                return;
            }

            if (obj is ISettings)
            {
                SettingsNode objNode = ((ISettings)obj).ConvertToSettingsNode();
                node.Nodes = objNode.Nodes;
                return;
            }

            if (IsListType(type))
            {
                IList list = obj as IList;

                node.ListLength = list.Count;

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i] == null)
                    {
                        node.AddChild("");
                        continue;
                    }

                    Type itemType = list[i].GetType();

                    if (itemType.IsPrimitive || itemType.IsEnum || itemType == typeof(string))
                    {
                        node.AddChild(list[i].ToString());
                        continue;
                    }

                    SettingsNode itemNode = node.AddChild(itemType.Name + " " + (i + 1), "", itemType);

                    GetNodes(list[i], itemType, itemNode);
                }

                return;
            }

            PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in propertyInfos)
            {
                if (pi == null)
                {
                    continue;
                }

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

                object value = pi.GetValue(obj, null);

                if (value == obj)
                {
                    return;
                }

                if (node.Level > 15)
                {
                    return;
                }

                string description        = "";
                DescriptionAttribute[] da = pi.GetCustomAttributes(typeof(DescriptionAttribute), true) as DescriptionAttribute[];

                if (da.Length > 0)
                {
                    description = da[0].Description;
                }

                SettingsNode nextChild = node.AddChild(pi.Name, description, pi.PropertyType);
                GetNodes(value, pi.PropertyType, nextChild);
            }
        }