System.Configuration.ConfigurationLockCollection 类

方法描述

包含锁定的配置对象的集合。 此类不能被继承。

语法定义(C# System.Configuration.ConfigurationLockCollection 类 的用法)

public sealed class ConfigurationLockCollection : ICollection, 
	IEnumerable

构造函数

构造函数名称 构造函数描述

成员/方法

方法名称 方法描述
Add 通过将配置对象添加到集合来锁定该配置对象。
Clear 清除集合中的所有的配置对象。
Contains 验证是否锁定了某个特定的配置对象。
CopyTo 从目标数组的指定索引处开始,将整个 ConfigurationLockCollection 集合复制到兼容的一维 Array 中。
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetEnumerator 获取 IEnumerator 对象,此对象用于循环访问此 ConfigurationLockCollection 集合。
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetType 获取当前实例的 Type。 (继承自 Object。)
IsReadOnly 验证某个特定的配置对象是否为只读。
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
Remove 从集合中移除配置对象。
SetFromList 根据所提供的列表锁定一组配置对象。
ToString 返回表示当前对象的字符串。 (继承自 Object。)

提示和注释

在配置文件中,配置节同时包含特性和元素。 对于配置节的锁定特性,存在一个 ConfigurationLockCollection 集合,该集合通过 ConfigurationElement 类的 LockAttributes 属性进行访问。 对于配置节的锁定元素,存在另一个 ConfigurationLockCollection 集合,该集合通过 ConfigurationElement 类的 LockElements 属性进行访问。

System.Configuration.ConfigurationLockCollection 类例子

下面的代码示例演示如何使用 ConfigurationLockCollection 类型。

#region Using directives

using System;
using System.Configuration;
using System.Web.Configuration;
using System.Collections;
using System.Text;

#endregion

namespace Samples.Aspnet.SystemWebConfiguration
{
  class UsingConfigurationLockCollection
  {
    static void Main(string[] args)
    {
      try
      {
        // Set the path of the config file.
        string configPath = "";

        // Get the Web application configuration object.
        Configuration config =
          WebConfigurationManager.OpenWebConfiguration(configPath);

        // Get the section related object.
        AnonymousIdentificationSection configSection =
          (AnonymousIdentificationSection)config.GetSection
          ("system.web/anonymousIdentification");

        // Display title and info.
        Console.WriteLine("Configuration Info");
        Console.WriteLine();

        // Display Config details.
        Console.WriteLine("File Path: {0}",
          config.FilePath);
        Console.WriteLine("Section Path: {0}",
          configSection.SectionInformation.Name);
        Console.WriteLine();

        // Create a ConfigurationLockCollection object.
        ConfigurationLockCollection lockedAttribList;
        lockedAttribList = configSection.LockAttributes;

        // Add an attribute to the lock collection.
        if (!lockedAttribList.Contains("enabled"))
        {
          lockedAttribList.Add("enabled");
        }
        if (!lockedAttribList.Contains("cookieless"))
        {
          lockedAttribList.Add("cookieless");
        }

        // Count property.
        Console.WriteLine("Collection Count: {0}",
         lockedAttribList.Count);

        // AttributeList method.
        Console.WriteLine("AttributeList: {0}",
         lockedAttribList.AttributeList);

        // Contains method.
        Console.WriteLine("Contains 'enabled': {0}",
         lockedAttribList.Contains("enabled"));

        // HasParentElements property.
        Console.WriteLine("HasParentElements: {0}",
         lockedAttribList.HasParentElements);

        // IsModified property.
        Console.WriteLine("IsModified: {0}",
         lockedAttribList.IsModified);

        // IsReadOnly method. 
        Console.WriteLine("IsReadOnly: {0}",
         lockedAttribList.IsReadOnly("enabled"));

        // Remove a configuration object 
        // from the collection.
        lockedAttribList.Remove("cookieless");

        // Clear the collection.
        lockedAttribList.Clear();

        // Create an ArrayList to contain
        // the property items of the configuration
        // section.
        ArrayList configPropertyAL = new ArrayList(lockedAttribList.Count);
        foreach (PropertyInformation propertyItem in
          configSection.ElementInformation.Properties)
        {
          configPropertyAL.Add(propertyItem.Name.ToString());
        }
        // Copy the elements of the ArrayList to a string array.
        String[] myArr = (String[])configPropertyAL.ToArray(typeof(string));
        // Create as a comma delimited list.
        string propList = string.Join(",", myArr);
        // Lock the items in the list.
        lockedAttribList.SetFromList(propList);
      }

      catch (Exception e)
      {
        // Unknown error.
        Console.WriteLine(e.ToString());
      }

      // Display and wait.
      Console.ReadLine();
    }
  }
}

继承层次结构

System.Object

System.Configuration.ConfigurationLockCollection

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。