C# 1.x 實現 “強型別元素唯一的 ArrayList”

來源:互聯網
上載者:User

//本文參閱 CSDN ccat 的 MarchLibrary 修改
// C# 1.x 實現 "強型別元素唯一的 ArrayList"
using System;
using System.Collections;

/// 任何元素的 Type 都應當等於指定的類型或為指定類型的子類。
/// 對於實值型別,最好先行裝箱再使用。
/// 作為 C# 1.x 語言實現泛型功能之前的代用品(執行階段錯誤)。
//方法1: 通過繼承於 System.Collections.ArrayList 實現
public class StrongTypeUniqueArrayList : ArrayList
{
 private Type _type;
 /// <summary>
 /// 禁止預設構造。
 /// </summary>
 private StrongTypeUniqueArrayList()
 {
 }
 /// <summary>
 /// 在容器初始化時指定其元素類型
 /// </summary>
 /// <param name="ElementType">ArrayList (元素)類型</param>
 public StrongTypeUniqueArrayList(System.Type ElementType)
 {
  _type = ElementType;
 }
 /// <summary>
 /// 不能再更改其內部元素類型。
 /// </summary>
 public Type Type
 {
  get
  {
   return _type;
  }
 }
 private bool IsMatchType(Type eType)
 {
  return (eType == _type)|(eType.IsSubclassOf(_type));
 }
 

 public override object this[int index]
 {
  set
  {
   if (IsMatchType(value.GetType()))
   {
    if (base.Contains(value))
    {
     if (base.IndexOf(value) == index)
     {
      base[index] = value;
     }
     else
     {
      throw new ArgumentException(value.ToString() + " 元素重複","value");
     }
    }
    else
    {
     base[index] = value;
    }
   }
   else
   {
    throw new ArgumentException(value.GetType().FullName + " 類型錯誤", "value");
   }
  }
 }

 public override int Add(object value)
 {
  if (!base.Contains(value) && IsMatchType(value.GetType()))
  {
   base.Add(value);
   return 1;
  }
  else
  {
   throw new ArgumentException(value.GetType().FullName + " 類型錯誤 或 " + value.ToString() + " 元素重複", "value");
   //return -1;
  }
 }
 public override void Insert(int index, object value)
 {
  if (!base.Contains(value) && IsMatchType(value.GetType()))
  {
   base.Insert(index,value);
  }
  else
  {
   throw new ArgumentException(value.GetType().FullName + " 類型錯誤 或 " + value.ToString() + " 元素重複", "value");
  }
 }

 public override void InsertRange(int index, ICollection c)
 {
  System.Collections.IEnumerator ie = c.GetEnumerator();
  while (ie.MoveNext())
  {
   if (base.Contains(ie.Current) || !IsMatchType(ie.Current.GetType()))
   {
    throw new ArgumentException(ie.Current.GetType().FullName + " 類型錯誤 或 " + ie.Current.ToString() + " 元素重複", "c");
   }
  }
  base.InsertRange(index,c);
 }
 public override void SetRange(int index, ICollection c)
 {
  System.Collections.IEnumerator ie = c.GetEnumerator();
  int i = 0;
  while (ie.MoveNext())
  {
   if (IsMatchType(ie.Current.GetType()))
   {
    if (base.Contains(ie.Current) && base.IndexOf(ie.Current) != i)
    {
     throw new ArgumentException(ie.Current.ToString() + " 元素重複","c");
    }
   }
   else
   {
    throw new ArgumentException(ie.Current.GetType().FullName + " 類型錯誤", "c");
   }
   i++;
  }
  base.SetRange(index,c);
 }
 public override void AddRange(ICollection c)
 {
  System.Collections.IEnumerator ie = c.GetEnumerator();
  while (ie.MoveNext())
  {
   if (base.Contains(ie.Current) || !IsMatchType(ie.Current.GetType()))
   {
    throw new ArgumentException(ie.Current.GetType().FullName + " 類型錯誤 或 " + ie.Current.ToString() + " 元素重複", "c");
   }
  }
  base.AddRange(c);
 }
}
//Test:
public class Class1
{
 private static int i = 0;
 public string xxx = "test";
 public Class1()
 {
  System.Console.WriteLine(i++);
 }
 static void Main(string[] args)
 {
  System.Console.WriteLine("Hello World");
  Class1 c1 = new Class1();
  Class1 c2 = new Class1();
  Class1 c3 = new Class1();
  Class1 c4 = new Class1();
  //StrongTypeUniqueArrayList x = new StrongTypeUniqueArrayList(typeof(Class1));
  StrongTypeUniqueArrayList x = new StrongTypeUniqueArrayList(System.Type.GetType("Class1"));
  System.Console.WriteLine(x.Type);
  x.Add(c1);
  x.Add(c2);
  x.Add(c3);
  x.Insert(0,c4);
  //x.Add(new Class1());
  //x.Add(c1);
//  x[2]= new Object();
  //x.Insert(2,new Object()); //類型錯誤
  //x.Insert(2,c2);
  //  x.Add(c2); //元素重複
 }
}

//方法2: 不通過繼承於 System.Collections.ArrayList 實現
//《Refactoring: Improving the Design of Existing Code》
// 3.21 Refused Bequest: Replace Inheritance with Delegation

using System;
using System.Collections;
public class Class1
{
 private static int i = 0;
 public string xxx = "test";
 public Class1()
 {
  System.Console.WriteLine(i++);
  xxx = i.ToString();
 }
 public override string ToString()
 {
  return xxx;
 }
 static void Main(string[] args)
 {
  System.Console.WriteLine("Hello World");
  System.Console.WriteLine("Hello World");
  Class1 c1 = new Class1();
  Class1 c2 = new Class1();
  Class1 c3 = new Class1();
  Class1 c4 = new Class1();
  StrongTypeUniqueArrayList x = new StrongTypeUniqueArrayList(System.Type.GetType("Class1"));
  System.Console.WriteLine(x.Type);
  x.Add(c1);
  x.Add(c2);
  x.Add(c3);
  x.Insert(0,c4);
  System.Collections.IEnumerator ie = x.GetEnumerator();
  while (ie.MoveNext())
   System.Console.WriteLine(ie.Current.ToString());
  x[0]= c4 ;
  while (ie.MoveNext())
   System.Console.WriteLine(ie.Current.ToString());
  x.Reset();
  foreach(Object o in x)
   System.Console.WriteLine(o.ToString() + "each");
 }
}

public class StrongTypeUniqueArrayList : System.Collections.IEnumerator//,System.Collections.IEnumerable // 支援 foreach
{
 private ArrayList al;
 private Type _type;
 private int _index = -1;

 public StrongTypeUniqueArrayList(System.Type ElementType)
 {
  al = new ArrayList();
  _type = ElementType;
 }

 public System.Collections.IEnumerator GetEnumerator() // 支援 迭代 和 foreach
 {
  return this;
 }

 public bool MoveNext() // 支援 foreach
 {
  
  return ++ _index < al.Count;
 }
 public void Reset() // 支援 foreach
 {
  _index = -1;
 }
 public Object Current // 支援 foreach
 {
  get
  {
   return al[_index];
  }
 }

 public Type Type
 {
  get
  {
   return _type;
  }
 }
 private bool IsMatchType(Type eType)
 {
  return (eType == _type)|(eType.IsSubclassOf(_type));
 }
 public object this[int index]
 {
  set
  {
   if (IsMatchType(value.GetType()))
   {
    if (al.Contains(value))
    {
     if (al.IndexOf(value) == index)
     {
      al[index] = value;
     }
     else
     {
      throw new ArgumentException(value.ToString() + " 元素重複","value");
     }
    }
    else
    {
     al[index] = value;
    }
   }
   else
   {
    throw new ArgumentException(value.GetType().FullName + " 類型錯誤", "value");
   }
  }
  get
  {
   return al[index];
  }
 }
 public int Add(object value)
 {
  if (!al.Contains(value) && IsMatchType(value.GetType()))
  {
   al.Add(value);
   return 1;
  }
  else
  {
   throw new ArgumentException(value.GetType().FullName + " 類型錯誤 或 " + value.ToString() + " 元素重複", "value");
  }
 }
 public void Insert(int index, object value)
 {
  if (!al.Contains(value) && IsMatchType(value.GetType()))
  {
   al.Insert(index,value);
  }
  else
  {
   throw new ArgumentException(value.GetType().FullName + " 類型錯誤 或 " + value.ToString() + " 元素重複", "value");
  }
 }
 public void InsertRange(int index, ICollection c)
 {
  System.Collections.IEnumerator ie = c.GetEnumerator();
  while (ie.MoveNext())
  {
   if (al.Contains(ie.Current) || !IsMatchType(ie.Current.GetType()))
   {
    throw new ArgumentException(ie.Current.GetType().FullName + " 類型錯誤 或 " + ie.Current.ToString() + " 元素重複", "c");
   }
  }
  al.InsertRange(index,c);
 }
 public void SetRange(int index, ICollection c)
 {
  System.Collections.IEnumerator ie = c.GetEnumerator();
  int i = 0;
  while (ie.MoveNext())
  {
   if (IsMatchType(ie.Current.GetType()))
   {
    if (al.Contains(ie.Current) && al.IndexOf(ie.Current) != i)
    {
     throw new ArgumentException(ie.Current.ToString() + " 元素重複","c");
    }
   }
   else
   {
    throw new ArgumentException(ie.Current.GetType().FullName + " 類型錯誤", "c");
   }
   i++;
  }
  al.SetRange(index,c);
 }
 public void AddRange(ICollection c)
 {
  foreach(Object o in al)
  {
   if (al.Contains(o) || !IsMatchType(o.GetType()))
   {
    throw new ArgumentException(o.GetType().FullName + " 類型錯誤 或 " + o.ToString() + " 元素重複", "c");
   }
  }
  al.AddRange(c);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.