欢迎光临
我们一直在努力

C# 1.x 实现 "强类型元素唯一的 ArrayList"-.NET教程,C#语言

建站超值云服务器,限时71元/月

//本文参阅 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); //元素重复

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C# 1.x 实现 "强类型元素唯一的 ArrayList"-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章