DNN学习笔记代码学习:ProviderConfiguration 荣

using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using System.Xml;

namespace WebApplication1
{
 /// <summary>
 /// 返回存储配置信息的对象,
 /// 通过LoadValuesFromConfigurationXml方法和配置信息来控制当前哈希表。
 /// </summary>
 public class ProviderConfiguration
 {
  /// <summary>
  /// 一个哈希表
  /// </summary>
  private Hashtable _Providers = new Hashtable();

  /// <summary>
  /// 配置元素的“defaultProvider”属性值
  /// </summary>
  private string _DefaultProvider;

  public ProviderConfiguration()
  {
  }

  /// <summary>
  /// 从配置文件的dotnetnuke元素中取得信息,并返回为ProviderConfiguration对象
  /// </summary>
  /// <param name="strProvider"></param>
  /// <returns></returns>
  public static ProviderConfiguration GetProviderConfiguration(string strProvider)
  {
   // 根据配置信息取得ProviderConfiguration对象
   return (ProviderConfiguration)ConfigurationSettings.GetConfig("dotnetnuke/" + strProvider);
  }

  public string DefaultProvider
  {
   get
   {
    return _DefaultProvider;
   }
  }

  public Hashtable Providers
  {
   get
   {
    return _Providers;
   }
  }

  /// <summary>
  ///
  /// </summary>
  /// <param name="node"></param>
  public void LoadValuesFromConfigurationXml(XmlNode node)
  {
   // 可以按名称和索引访问的属性集合
   XmlAttributeCollection attributeCollection = node.Attributes;

   // 给DefaultProvider赋值
   _DefaultProvider = attributeCollection["defaultProvider"].Value;

   // 循环子节点
   foreach (XmlNode child in node.ChildNodes)
   {
    if (child.Name == "providers")
    {
     // 根据配置信息来操作哈希表_Providers
     GetProviders(child);
    }
   }
  }

  /// <summary>
  /// 根据配置信息来操作哈希表_Providers。
  /// </summary>
  /// <param name="node">Xml文档节点</param>
  public void GetProviders(XmlNode node)
  {
   foreach(XmlNode provider in node)
   {
    switch (provider.Name)
    {
      // 根据配置信息,向缓存中添加新元素。
      //将生成的Provider对象作为值,name属性值作为键值,添加到哈希表中
     case "add":
      // 将带有指定键和值的元素添加到哈希表_Providers中
      Providers.Add(provider.Attributes["name"].Value, new Provider(provider.Attributes));
      break;

      //  根据配置信息,向缓存中移除元素
      //  从 System.Collections.Hashtable 中移除键值为name属性值的元素。
     case "remove":
      // 将带有指定键和值的元素从哈希表_Providers中移除
      Providers.Remove(provider.Attributes["name"].Value);
      break;

      //  根据配置信息,清除缓存中的元素
      //  从 System.Collections.Hashtable 中移除所有元素。
     case "clear":
      // 移除哈希表_Providers中的所有元素
      Providers.Clear();
      break;
    }
   }
  }
 }
}

原文地址:https://www.cnblogs.com/admin11/p/193290.html