CSHashTable

来源:互联网 发布:突击英雄知乎 编辑:程序博客网 时间:2024/06/05 00:54

[code]

<%@ CodeTemplate Debug="true" Language="C#" TargetLanguage="C#" Description="Generates a strongly-typed collection based on a HashTable." %>
<%@ Property Name="ClassNamespace" Type="System.String" Optional="True" Category="Context" Description="The namespace that the generated class will be a member of." %>
<%@ Property Name="KeyType" Type="System.String" Category="Context" Description="The type to use as a key in the collection." %>
<%@ Property Name="ItemType" Type="System.String" Category="Context" Description="The type to use as an item in the collection." %>
<%@ Property Name="ClassName" Type="System.String" Category="Context" Description="The name of the class to be generated." %>
<%@ Property Name="Accessibility" Type="AccessibilityEnum" Category="Options" Description="The accessibility of the class to be generated." %>
using System;
using System.Collections;<% if (ClassNamespace != null && ClassNamespace.Length > 0) { %>
namespace <%= ClassNamespace %>
{
<% } %>
 <%= GetAccessModifier(Accessibility) %> class <%= ClassName %> : IDictionary, ICollection, IEnumerable, ICloneable
 {
  protected Hashtable innerHash;
  
  #region "Constructors"
  public  <%= ClassName %>()
  {
   innerHash = new Hashtable();
  }
  
  public <%= ClassName %>(<%= ClassName %> original)
  {
   innerHash = new Hashtable (original.innerHash);
  }
  
  public <%= ClassName %>(IDictionary dictionary)
  {
   innerHash = new Hashtable (dictionary);
  }
  
  public <%= ClassName %>(int capacity)
  {
   innerHash = new Hashtable(capacity);
  }
  
  public <%= ClassName %>(IDictionary dictionary, float loadFactor)
  {
   innerHash = new Hashtable(dictionary, loadFactor);
  }
  
  public <%= ClassName %>(IHashCodeProvider codeProvider, IComparer comparer)
  {
   innerHash = new Hashtable (codeProvider, comparer);
  }
  
  public <%= ClassName %>(int capacity, int loadFactor)
  {
   innerHash = new Hashtable(capacity, loadFactor);
  }
  
  public <%= ClassName %>(IDictionary dictionary, IHashCodeProvider codeProvider, IComparer comparer)
  {
   innerHash = new Hashtable (dictionary, codeProvider, comparer);
  }
  
  public <%= ClassName %>(int capacity, IHashCodeProvider codeProvider, IComparer comparer)
  {
   innerHash = new Hashtable (capacity, codeProvider, comparer);
  }
  
  public <%= ClassName %>(IDictionary dictionary, float loadFactor, IHashCodeProvider codeProvider, IComparer comparer)
  {
   innerHash = new Hashtable (dictionary, loadFactor, codeProvider, comparer);
  }
  
  public <%= ClassName %>(int capacity, float loadFactor, IHashCodeProvider codeProvider, IComparer comparer)
  {
   innerHash = new Hashtable (capacity, loadFactor, codeProvider, comparer);
  }
  #endregion

  #region Implementation of IDictionary
  public <%= ClassName %>Enumerator GetEnumerator()
  {
   return new <%= ClassName %>Enumerator(this);
  }
         
  System.Collections.IDictionaryEnumerator IDictionary.GetEnumerator()
  {
   return new <%= ClassName %>Enumerator(this);
  }
  
  IEnumerator IEnumerable.GetEnumerator()
  {
   return GetEnumerator();
  }
  
  public void Remove(<%= KeyType %> key)
  {
   innerHash.Remove (key);
  }
  
  void IDictionary.Remove(object key)
  {
   Remove ((<%= KeyType %>)key);
  }
  
  public bool Contains(<%= KeyType %> key)
  {
   return innerHash.Contains(key);
  }
  
  bool IDictionary.Contains(object key)
  {
   return Contains((<%= KeyType %>)key);
  }
  
  public void Clear()
  {
   innerHash.Clear();  
  }
  
  public void Add(<%= KeyType %> key, <%= ItemType %> value)
  {
   innerHash.Add (key, value);
  }
  
  void IDictionary.Add(object key, object value)
  {
   Add ((<%= KeyType %>)key, (<%= ItemType %>)value);
  }
  
  public bool IsReadOnly
  {
   get
   {
    return innerHash.IsReadOnly;
   }
  }
  
  public <%= ItemType %> this[<%= KeyType %> key]
  {
   get
   {
    return (<%= ItemType %>) innerHash[key];
   }
   set
   {
    innerHash[key] = value;
   }
  }
  
  object IDictionary.this[object key]
  {
   get
   {
    return this[(<%= KeyType %>)key];
   }
   set
   {
    this[(<%= KeyType %>)key] = (<%= ItemType %>)value;
   }
  }
         
  public System.Collections.ICollection Values
  {
   get
   {
    return innerHash.Values;
   }
  }
  
  public System.Collections.ICollection Keys
  {
   get
   {
    return innerHash.Keys;
   }
  }
  
  public bool IsFixedSize
  {
   get
   {
    return innerHash.IsFixedSize;
   }
  }
  #endregion
  
  #region Implementation of ICollection
  public void CopyTo(System.Array array, int index)
  {
   innerHash.CopyTo (array, index);
  }
  
  public bool IsSynchronized
  {
   get
   {
    return innerHash.IsSynchronized;
   }
  }
  
  public int Count
  {
   get
   {
    return innerHash.Count;
   }
  }
  
  public object SyncRoot
  {
   get
   {
    return innerHash.SyncRoot;
   }
  }
  #endregion
  
  #region Implementation of ICloneable
  public <%= ClassName %> Clone()
  {
   <%= ClassName %> clone = new <%= ClassName %>();
   clone.innerHash = (Hashtable) innerHash.Clone();
   
   return clone;
  }
  
  object ICloneable.Clone()
  {
   return Clone();
  }
  #endregion
  
  #region "HashTable Methods"
  public bool ContainsKey (<%= KeyType %> key)
  {
   return innerHash.ContainsKey(key);
  }
  
  public bool ContainsValue (<%= ItemType %> value)
  {
   return innerHash.ContainsValue(value);
  }
  
  public static <%= ClassName %> Synchronized(<%= ClassName %> nonSync)
  {
   <%= ClassName %> sync = new <%= ClassName %>();
   sync.innerHash = Hashtable.Synchronized(nonSync.innerHash);

   return sync;
  }
  #endregion

  internal Hashtable InnerHash
  {
   get
   {
    return innerHash;
   }
  }
 }
 
 public class <%= ClassName %>Enumerator : IDictionaryEnumerator
 {
  private IDictionaryEnumerator innerEnumerator;
  
  internal <%= ClassName %>Enumerator (<%= ClassName %> enumerable)
  {
   innerEnumerator = enumerable.InnerHash.GetEnumerator();
  }
  
  #region Implementation of IDictionaryEnumerator
  public <%= KeyType %> Key
  {
   get
   {
    return (<%= KeyType %>)innerEnumerator.Key;
   }
  }
  
  object IDictionaryEnumerator.Key
  {
   get
   {
    return Key;
   }
  }
  
  public <%= ItemType %> Value
  {
   get
   {
    return (<%= ItemType %>)innerEnumerator.Value;
   }
  }
  
  object IDictionaryEnumerator.Value
  {
   get
   {
    return Value;
   }
  }
  
  public System.Collections.DictionaryEntry Entry
  {
   get
   {
    return innerEnumerator.Entry;
   }
  }
  #endregion
  
  #region Implementation of IEnumerator
  public void Reset()
  {
   innerEnumerator.Reset();
  }
  
  public bool MoveNext()
  {
   return innerEnumerator.MoveNext();
  }
  
  public object Current
  {
   get
   {
    return innerEnumerator.Current;
   }
  }
  #endregion
 }
<% if (ClassNamespace != null && ClassNamespace.Length > 0) { %>
}
<% } %>
<script runat="template">
public enum AccessibilityEnum
{
 Public,
 Protected,
 Internal,
 ProtectedInternal,
 Private
}

public string GetAccessModifier(AccessibilityEnum accessibility)
{
 switch (accessibility)
 {
  case AccessibilityEnum.Public: return "public";
  case AccessibilityEnum.Protected: return "protected";
  case AccessibilityEnum.Internal: return "internal";
  case AccessibilityEnum.ProtectedInternal: return "protected internal";
  case AccessibilityEnum.Private: return "private";
  default: return "public";
 }
}
</script>

 [/code]