OK,其实很简单,我效仿DataColumnCollection,DataRowCollection等类构造了一个自已使用的OperatorCollection(人员集合类)using System;namespace test
{
/// <summary>
/// OperatorCollection 的摘要说明。
/// </summary>
[Serializable]
public class OperatorCollection: System.Data.InternalDataCollectionBase
{ private System.Collections.ArrayList aList;
protected override System.Collections.ArrayList List
{
get
{
if (aList == null) 
{
aList = new System.Collections.ArrayList();
}
return aList;
}
} public OperatorCollection()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public int Count{
get
{
return List.Count;
}
}
/// <summary>
/// 索引器
/// </summary>
public Operator this[int index]
{
get
{
return (Operator)List[index];
}
} public void Add(Operator aOperator) 
{
List.Add(aOperator);
} public void Remove(Operator aOperator) 
{
List.Remove(aOperator);
} public void RemoveAt(int index) 
{
List.RemoveAt(index);
} public bool Contains(Object item) 
{
return List.Contains(item);
} public void Clear() 
{
List.Clear();
} }
}注意Operator是我自定义的类,类本身的序列化没有问题。这个类只要不继承于InternalDataCollectionBase的序列化也不会出问题,
序列化出问题的主要测试方法是放在Asp.net页面中的ViewState或Session中测试一下,只要实现序列化的对象才能放入。

解决方案 »

  1.   

    我拿你的代码运行了一下,出现的错误是:System.Data.InternalDataCollectionBase 未标记为可序列化。
    我想一个解决办法是使用接口ICollection, IEnumerable代替InternalDataCollectionBase,自己实现接口的方法。我以前看过的一篇文章http://www.codeproject.com/cs/combobox/ImageListBox.asp,它也是使用这种方法来实现ImageListBox.Items属性的序列化。 [Serializable]
    public class OperatorCollection: ICollection, IEnumerable
    { private System.Collections.ArrayList aList; public OperatorCollection()
    {
        aList=new ArrayList();
    } protected  System.Collections.ArrayList List
    {
    get
    {
    return aList;
    }
    } public void CopyTo(Array ar,int index)
    {
    aList.CopyTo(ar,index);
    }
    public bool IsSynchronized
    {
    get {return aList.IsSynchronized;}
    } public Object SyncRoot
    {
    get {return aList.SyncRoot;}
    }
    public IEnumerator GetEnumerator()
    {
    return aList.GetEnumerator();
    }
    public int Count
    {
    get
    {
    return List.Count;
    }
    }
    /// <summary>
    /// 索引器
    /// </summary>
    public Operator this[int index]
    {
    get
    {
    return (Operator)List[index];
    }
    } public void Add(Operator aOperator) 
    {
    List.Add(aOperator);
    } public void Remove(Operator aOperator) 
    {
    List.Remove(aOperator);
    } public void RemoveAt(int index) 
    {
    List.RemoveAt(index);
    } public bool Contains(Object item) 
    {
    return List.Contains(item);
    } public void Clear() 
    {
    List.Clear();
    } }
      

  2.   

    多谢,看来只好这样,其实很奇怪,M$的DataRowCollection及DataColumnCollection均继承于些类,但却不出现该问题。