我想写一个集合类,像datarow一样“dr[dc].xxxx”调用datacolumn
请问怎样写呢?谢谢!!

解决方案 »

  1.   

    大致如下:class MyList:CollectionBase
    {
          public string this[int index]
        {
           get
           {
              return (string)List[index];
            }
        }
    }[====我的精英团队====]    www.51team.com
      

  2.   

    从CollectionBase中派生自己的类是最简单的方法,
    派生后要重载一些属性和方法,具体的看MSDN,查CollectionBase就可以,里面有一个详细的例子。
      

  3.   

    using System;
    using System.Collections;namespace RaulRedondo.PatternRecognition.Common
    {
    /// <summary>
    /// ClusterCollection &micro;&Auml;&Otilde;&ordf;&Ograve;&ordf;&Euml;&micro;&Atilde;÷&iexcl;&pound;
    /// </summary>
    public class ClusterCollection : CollectionBase
    {
    public Cluster Add(Cluster value)
    {
    this.List.Add(value);
    return value;
    } public void AddRange(Cluster[] values)
    {
    foreach(Cluster p in values)
    this.List.Add(p);
    } public Cluster this[int index]
    {
    get
    {
    return this.List[index] as Cluster;
    }
    set
    {
    this.List[index] = value;
    }
    } public int IndexOf(Cluster value )  
    {
    return this.List.IndexOf(value);
    } public void Insert(int index, Cluster value)  
    {
    this.List.Insert(index, value );
    } public void Remove(Cluster value )  
    {
    this.List.Remove( value );
    } public bool Contains(Cluster value )  
    {
    return this.List.Contains(value);
    } protected override void OnInsert(int index, Object value )  
    {
    if (value.GetType() != typeof(Cluster))
    throw new ArgumentException("value must be of type Cluster.", "value");
    } protected override void OnRemove(int index, Object value )  
    {
    if (value.GetType() != typeof(Cluster))
    throw new ArgumentException("value must be of type Cluster.", "value");
    } protected override void OnSet(int index, Object oldValue, Object newValue)  
    {
    if (newValue.GetType() != typeof(Cluster))
    throw new ArgumentException("newValue must be of type Cluster.", "newValue");
    } protected override void OnValidate(Object value )  
    {
    if (value.GetType() != typeof(Cluster))
    throw new ArgumentException("value must be of type Cluster.");
    }
    }
    }
    你把类名查找替换就可以了