コード例 #1
0
        private void treeConfig_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (!(e.Node is ExTreeNode))
            {
                return;
            }

            ExTreeNode eNode = e.Node as ExTreeNode;

            if (eNode.ChildInitFlag)
            {
                return;
            }

            eNode.ChildInitFlag = true;

            if (object.ReferenceEquals(null, eNode.Tag) || !(eNode.Tag is ConfigurationSectionGroup))
            {
                return;
            }

            ConfigurationSectionGroup group = eNode.Tag as ConfigurationSectionGroup;
            object obj = null;
            IEnumerator <PropertyInfo> enumerator = null;
            TreeNode node = null;

            eNode.Nodes.Clear();

            //处理定义的所有配置组单元
            enumerator = ConfigurationHelper.GetDeclaredSubSectionGroups(group.GetType());
            while (enumerator.MoveNext())
            {
                obj              = ConfigurationHelper.GetConfigurationSectionGroup(group, enumerator.Current.Name, enumerator.Current.PropertyType, true, true);
                node             = NewConfigurationSectionGroupNode(enumerator.Current.Name, obj);
                node.ToolTipText = GetDescription(enumerator.Current);
                e.Node.Nodes.Add(eNode);
            }

            //处理定义的所有配置段单元
            enumerator = ConfigurationHelper.GetDeclaredSubSections(group.GetType());
            while (enumerator.MoveNext())
            {
                obj              = ConfigurationHelper.GetConfigurationSection(group, enumerator.Current.Name, enumerator.Current.PropertyType, true, true);
                node             = NewConfigurationSectionNode(enumerator.Current.Name, obj);
                node.ToolTipText = GetDescription(enumerator.Current);
                e.Node.Nodes.Add(node);
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取配置组中定义的所有子配置段信息
        /// </summary>
        /// <param name="group">配置组</param>
        /// <returns>包含配置组中声明的所有ConfigurationSection属性信息的迭代器</returns>
        public static IEnumerator <PropertyInfo> GetDeclaredSubSections(ConfigurationSectionGroup group)
        {
            if (object.ReferenceEquals(null, group))
            {
                return(null);
            }

            return(GetDeclaredSubSections(group.GetType()));
        }
コード例 #3
0
        private static T GetSectionGroupInstance <T>(string sectionGroupName) where T : ConfigurationSectionGroup
        {
            if (string.IsNullOrEmpty(sectionGroupName))
            {
                return(Activator.CreateInstance <T>());
            }

            System.Configuration.Configuration appConfig =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ConfigurationSectionGroup instance = appConfig.GetSectionGroup(sectionGroupName);

            return((instance == null || !typeof(T).IsAssignableFrom(instance.GetType()))
                ? Activator.CreateInstance <T>()
                : instance as T);
        }
コード例 #4
0
        /// <summary>
        /// 从配置组集合中获取一个配置组
        /// </summary>
        /// <typeparam name="T">要获取的配置组类型</typeparam>
        /// <param name="groups">配置组集合</param>
        /// <param name="groupName">要获取的配置组名称</param>
        /// <param name="createWhenNonExist">不存在时创建</param>
        /// <param name="replaceWhenTypeConflict">获取到的配置组类型与指定类型不一致时是否覆盖</param>
        /// <returns>指定的配置组</returns>
        public static ConfigurationSectionGroup GetConfigurationSectionGroup(ConfigurationSectionGroup fatherConfigGroup, string groupName, Type groupType, bool createWhenNonExist, bool replaceWhenTypeConflict)
        {
            if (object.ReferenceEquals(null, fatherConfigGroup) || object.ReferenceEquals(null, groupType) || object.ReferenceEquals(null, groupName) || groupName.Length == 0)
            {
                return(null);
            }

            ConfigurationSectionGroup group = null;
            bool typeConflict = false;

            try
            {
                group = fatherConfigGroup.SectionGroups[groupName];

                if (!object.ReferenceEquals(null, group))
                {
                    if (group.GetType().Equals(groupType))
                    {
                        return(group);
                    }
                    else
                    {
                        typeConflict = true;
                        group        = null;
                    }
                }
            }
            catch
            {
                typeConflict = true;
            }

            //根据参数创建新配置项
            if (createWhenNonExist || (typeConflict && replaceWhenTypeConflict))
            {
                RemoveConfigUnit(fatherConfigGroup, groupName);
                group = CreateInstance(groupType) as ConfigurationSectionGroup;
                if (!object.ReferenceEquals(null, group))
                {
                    fatherConfigGroup.SectionGroups.Add(groupName, group);
                    FillConfigurationSectionGroup(group, true);
                }
            }

            return(group);
        }