This article see Marchlibrary modification of CSDN Ccat
C # 1.x implements the ArrayList of strongly typed elements
Using System;
Using System.Collections;
The type of any element should be equal to the specified type or to a subclass of the specified type.
For value types, it is best to use boxing in advance.
A substitute (Run-time error) for implementing generic functionality as a C # 1.x language.
public class Strongtypeuniquearraylist:arraylist
{
Private Type _type;
<summary>
Prevents default constructs.
</summary>
Private Strongtypeuniquearraylist ()
{
}
<summary>
Specifying its element type when the container is initialized
</summary>
<param name= "ElementType" >arraylist (Element) type </param>
Public strongtypeuniquearraylist (System.Type ElementType)
{
_type = ElementType;
}
<summary>
The intrinsic element type cannot be changed again.
</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 () + "element Repeat", "value");
}
}
Else
{
Base[index] = value;
}
}
Else
{
throw new ArgumentException (value. GetType (). FullName + "type error", "value");
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.