如何通过C#1.x实现quot?
//本文参阅 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 Inheritancewith 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);
}
}
本文地址:http://www.45fan.com/a/question/67075.html