• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

C# 1.x 实现 强类型元素唯一的 ArrayList

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 23次 | 进入软件测试论坛讨论

领测软件测试网 //本文参阅 CSDN ccat 的 MarchLibrary 修改
// C# 1.x 实现 "强类型元素唯一的 ArrayList"
using System;
using System.Collections;

/// 任何元素的 Type 都应当等于指定的类型或为指定类型的子类。
/// 对于值类型,最好先行装箱再使用。
/// 作为 C# 1.x 语言实现泛型功能之前的代用品(运行时错误)。

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); //元素重复
}
}


文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网