/// <summary>
 /// Overrides the properties for the configuration element.
 /// </summary>
 /// <typeparam name="T">The base type for the configuration elements collection.</typeparam>
 /// <param name="element">The configuration element.</param>
 /// <param name="subProvider">The <see cref="ConfigurationElementManageabilityProvider"/> used to override the element's
 /// properties.</param>
 /// <param name="readGroupPolicies"><see langword="true"/> if Group Policy overrides must be applied; otherwise,
 /// <see langword="false"/>.</param>
 /// <param name="machineKey">The <see cref="IRegistryKey"/> which holds the Group Policy overrides for the
 /// configuration section at the machine level, or <see langword="null"/>
 /// if there is no such registry key.</param>
 /// <param name="userKey">The <see cref="IRegistryKey"/> which holds the Group Policy overrides for the
 /// configuration section at the user level, or <see langword="null"/>
 /// if there is no such registry key.</param>
 /// <returns><see langword="true"/> if the policy settings do not disable the configuration element, otherwise
 /// <see langword="false"/>.</returns>
 /// <remarks>
 /// This method assumes a specific layout for the policy values: there is a registry key representing the collection
 /// of elements, and a sub key with the policy values for each element. An element's sub key may also contains a value
 /// stating whether the policy for an element is disabled; in that case the element is removed from the collection.
 /// Such a layout for the policy values can be constructed manually, or method
 /// <see cref="ConfigurationSectionManageabilityProvider.AddElementsPolicies{T}"/> can be invoked during the construction
 /// of the ADM template to generate it.
 /// </remarks>
 protected static bool OverrideWithGroupPolicies <T>(T element,
                                                     ConfigurationElementManageabilityProvider subProvider,
                                                     bool readGroupPolicies,
                                                     IRegistryKey machineKey,
                                                     IRegistryKey userKey)
     where T : NamedConfigurationElement, new()
 {
     return(subProvider.OverrideWithGroupPolicies(element,
                                                  readGroupPolicies, machineKey, userKey));
 }
        protected void OverrideWithGroupPoliciesForElementCollection <T>(NamedElementCollection <T> elements,
                                                                         String keyName,
                                                                         bool readGroupPolicies,
                                                                         IRegistryKey machineKey,
                                                                         IRegistryKey userKey)
            where T : NamedConfigurationElement, new()
        {
            List <T> elementsToRemove = new List <T>();

            IRegistryKey machineElementsKey = null;
            IRegistryKey userElementsKey    = null;

            try
            {
                LoadRegistrySubKeys(keyName,
                                    machineKey, userKey,
                                    out machineElementsKey, out userElementsKey);

                foreach (T element in elements)
                {
                    IRegistryKey machineElementKey = null;
                    IRegistryKey userElementKey    = null;

                    try
                    {
                        LoadRegistrySubKeys(element.Name,
                                            machineElementsKey, userElementsKey,
                                            out machineElementKey, out userElementKey);

                        ConfigurationElementManageabilityProvider subProvider = GetSubProvider(element.GetType());

                        if (subProvider != null && !OverrideWithGroupPolicies <T>(element,
                                                                                  subProvider,
                                                                                  readGroupPolicies, machineElementKey, userElementKey))
                        {
                            elementsToRemove.Add(element);
                        }
                    }
                    finally
                    {
                        ReleaseRegistryKeys(machineElementKey, userElementKey);
                    }
                }
            }
            finally
            {
                ReleaseRegistryKeys(machineElementsKey, userElementsKey);
            }

            // remove disabled elements
            foreach (T element in elementsToRemove)
            {
                elements.Remove(element.Name);
            }
        }
 /// <summary>
 /// Adds the ADM instructions that describe the policies that can be used to override the configuration
 /// information for the element using the supplied element manageability provider.
 /// </summary>
 /// <typeparam name="T">The base type for the configuration element.</typeparam>
 /// <param name="contentBuilder">The <see cref="AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
 /// <param name="element">The configuration element.</param>
 /// <param name="subProvider">The <see cref="ConfigurationElementManageabilityProvider"/> used to append the ADM instructions
 /// for the element.</param>
 /// <param name="configurationSource">The configuration source from where to get additional configuration
 /// information, if necessary.</param>
 /// <param name="parentKey">The key path for which the generated instructions' keys must be sub keys of.</param>
 protected static void AddAdministrativeTemplateDirectivesForElement <T>(AdmContentBuilder contentBuilder,
                                                                         T element,
                                                                         ConfigurationElementManageabilityProvider subProvider,
                                                                         IConfigurationSource configurationSource,
                                                                         String parentKey)
     where T : NamedConfigurationElement, new()
 {
     subProvider.AddAdministrativeTemplateDirectives(contentBuilder,
                                                     element,
                                                     configurationSource,
                                                     parentKey);
 }
        protected void AddElementsPolicies <T>(AdmContentBuilder contentBuilder,
                                               NamedElementCollection <T> elements,
                                               IConfigurationSource configurationSource,
                                               String parentKey,
                                               String categoryName)
            where T : NamedConfigurationElement, new()
        {
            contentBuilder.StartCategory(categoryName);

            foreach (T element in elements)
            {
                ConfigurationElementManageabilityProvider subProvider = GetSubProvider(element.GetType());

                if (subProvider != null)
                {
                    AddAdministrativeTemplateDirectivesForElement <T>(contentBuilder,
                                                                      element, subProvider,
                                                                      configurationSource,
                                                                      parentKey);
                }
            }

            contentBuilder.EndCategory();
        }
 /// <summary>
 /// Overrides the properties for the configuration element and creates the
 /// <see cref="ConfigurationSetting"/> instances that describe it.
 /// </summary>
 /// <typeparam name="T">The base type for the configuration elements collection.</typeparam>
 /// <param name="element">The configuration element.</param>
 /// <param name="subProvider">The <see cref="ConfigurationElementManageabilityProvider"/> used to override the element's
 /// properties and create the wmi objects.</param>
 /// <param name="readGroupPolicies"><see langword="true"/> if Group Policy overrides must be applied; otherwise,
 /// <see langword="false"/>.</param>
 /// <param name="machineKey">The <see cref="IRegistryKey"/> which holds the Group Policy overrides for the
 /// configuration section at the machine level, or <see langword="null"/>
 /// if there is no such registry key.</param>
 /// <param name="userKey">The <see cref="IRegistryKey"/> which holds the Group Policy overrides for the
 /// configuration section at the user level, or <see langword="null"/>
 /// if there is no such registry key.</param>
 /// <param name="generateWmiObjects"><see langword="true"/> if WMI objects must be generated; otherwise,
 /// <see langword="false"/>.</param>
 /// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
 /// <returns><see langword="true"/> if the policy settings do not disable the configuration element, otherwise
 /// <see langword="false"/>.</returns>
 /// <remarks>
 /// This method assumes a specific layout for the policy values: there is a registry key representing the collection
 /// of elements, and a sub key with the policy values for each element. An element's sub key may also contains a value
 /// stating whether the policy for an element is disabled; in that case the element is removed from the collection.
 /// Such a layout for the policy values can be constructed manually, or method
 /// <see cref="ConfigurationSectionManageabilityProvider.AddElementsPolicies{T}"/> can be invoked during the construction
 /// of the ADM template to generate it.
 /// </remarks>
 protected static bool OverrideWithGroupPoliciesAndGenerateWmiObjectsForElement <T>(T element,
                                                                                    ConfigurationElementManageabilityProvider subProvider,
                                                                                    bool readGroupPolicies,
                                                                                    IRegistryKey machineKey,
                                                                                    IRegistryKey userKey,
                                                                                    bool generateWmiObjects,
                                                                                    ICollection <ConfigurationSetting> wmiSettings)
     where T : NamedConfigurationElement, new()
 {
     return(subProvider.OverrideWithGroupPoliciesAndGenerateWmiObjects(element,
                                                                       readGroupPolicies, machineKey, userKey,
                                                                       generateWmiObjects, wmiSettings));
 }