【问题标题】:Why can't my IList implementation be used as a dataSource为什么我的 IList 实现不能用作数据源
【发布时间】:2014-03-27 14:54:01
【问题描述】:

我想将我的对象taskCollection 绑定到我的checkListBoxtaskCollection 实现了接口IList<Task>

当我尝试将其用作数据源时出现错误

System.ArgumentException : Complex DataBinding 接受 IList 或 IListSource 作为数据源。

这是我的TaskCollection

using System;
using System.Collections.Generic;
using System.IO;

namespace CLBatchGUI
{
    /// <summary>
    /// Description of TaskCollection.
    /// </summary>
    public class TaskCollection:IList<Task>
    {
        private List<Task> _tasks;

        public TaskCollection()
        {
            _tasks = new List<Task>();
        }

        public void executeTasks(){
        }

        public static void loadFromCSV(string path){
        }

        public void saveToCSV(string path){
            StreamWriter sw= new StreamWriter(path);
            saveToCSV(sw);
        }

        public void saveToCSV(TextWriter sw){
            if(_tasks.Count==0)
                throw new ApplicationException("There are no tasks to save");
            foreach (var Task in _tasks) {
                Task.validate();
            }

            try{
                sw.Write("TaskName,");
                for(int i=0;i<_tasks[0].job.parameters.Count;i++){
                    sw.Write(_tasks[0].job.parameters[i].Name);
                    if(i==_tasks.Count-1)
                        sw.Write("\n");
                    else
                        sw.Write(",");
                }
                for(int i=0;i<_tasks.Count;i++){
                    sw.Write(_tasks[i].Name +",");
                    for(int n=0;n<_tasks[i].job.parameters.Count;n++){
                        sw.Write(_tasks[i].job.parameters[n].Text);
                        if(n==_tasks[i].job.parameters.Count-1)
                            sw.Write("\n");
                        else
                            sw.Write(",");
                    }


                }
            }catch(Exception ex){
                throw new ApplicationException(ex.Message);
            }

        }

        public Task this[int index]{
            get{
                return _tasks[index];
            }
            set{
                _tasks[index]=value;
            }
        }

        public int Count {
            get {
                return _tasks.Count;
            }
        }

        public bool IsReadOnly {
            get {
                return false;
            }
        }

        public int IndexOf(Task item)
        {
            return _tasks.IndexOf(item);
        }

        public void Insert(int index, Task item)
        {
            _tasks.Insert(index,item);
        }

        public void RemoveAt(int index)
        {
            _tasks.RemoveAt(index);
        }

        public void Add(Task item)
        {
            _tasks.Add(item);
        }

        public void Clear()
        {
            _tasks.Clear();
        }

        public bool Contains(Task item)
        {
            return _tasks.Contains(item);
        }

        public void CopyTo(Task[] array, int arrayIndex)
        {
            _tasks.CopyTo(array,arrayIndex);
        }

        public bool Remove(Task item)
        {
            return _tasks.Remove(item);
        }

        public IEnumerator<Task> GetEnumerator()
        {
            return _tasks.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _tasks.GetEnumerator();
        }
    }
}

更新类:

 using System;
 using System.Collections.Generic;
 using System.IO;

 namespace CLBatchGUI
 {
/// <summary>
/// Description of TaskCollection.
/// </summary>
public class TaskCollection:IList<Task>,IList
{
    private List<Task> _tasks;
    public TaskCollection()
    {
        _tasks= new List<Task>();
    }

    public void executeTasks(){
    }

    public static void loadFromCSV(string path){

    }

    public void saveToCSV(string path){
        StreamWriter sw= new StreamWriter(path);
        saveToCSV(sw);
    }

    public void saveToCSV(TextWriter sw){
        if(_tasks.Count==0)
            throw new ApplicationException("There are no tasks to save");
        foreach (var Task in _tasks) {
            Task.validate();
        }

        try{
            sw.Write("TaskName,");
            for(int i=0;i<_tasks[0].job.parameters.Count;i++){
                sw.Write(_tasks[0].job.parameters[i].Name);
                if(i==_tasks.Count-1)
                    sw.Write("\n");
                else
                    sw.Write(",");
            }
            for(int i=0;i<_tasks.Count;i++){
                sw.Write(_tasks[i].Name +",");
                for(int n=0;n<_tasks[i].job.parameters.Count;n++){
                    sw.Write(_tasks[i].job.parameters[n].Text);
                    if(n==_tasks[i].job.parameters.Count-1)
                        sw.Write("\n");
                    else
                        sw.Write(",");
                }
            }
        }catch(Exception ex){
            throw new ApplicationException(ex.Message);
        }
    }

    public object this[int index]{
        get{
            return _tasks[index];
        }
        set{
            _tasks[index]=value;
        }
    }

    int IList.Count {
        get {
            return _tasks.Count;
        }
    }

    bool IList.IsReadOnly {
        get {
            return false;
        }
    }

    int IList.IndexOf(object item)
    {
        return _tasks.IndexOf(item);
    }

    void IList.Insert(int index, object item)
    {
        _tasks.Insert(index,item);
    }

    void IList.RemoveAt(int index)
    {
        _tasks.RemoveAt(index);
    }

    void IList.Add(object item)
    {
        _tasks.Add(item);
    }

    void IList.Clear()
    {
        _tasks.Clear();
    }

    bool IList.Contains(object item)
    {
        return _tasks.Contains(item);
    }

    void IList.CopyTo(object[] array, int arrayIndex)
    {
        _tasks.CopyTo(array,arrayIndex);
    }

    bool IList.Remove(object item)
    {
        return _tasks.Remove(item);
    }

 //     IList.IEnumerator<object> GetEnumerator()
 //     {
 //         return _tasks.GetEnumerator();
 //     }

 //     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 //     {
 //         return _tasks.GetEnumerator();
 //     }
}
 }

【问题讨论】:

  • IList&lt;T&gt; 实现 IEnumerable 但不是 IList,它是 IEnumerable 的子代。

标签: c# objectdatasource


【解决方案1】:

你的类必须实现IListIListSource,正如错误消息告诉你的那样。

您发布的类实现了IList&lt;T&gt;,但没有实现IList

所以你必须自己实现IList。您可以明确地执行此操作,因此您不会意外添加不是 Taskobject(因为将隐藏非泛型 Add 方法),只要您不明确地转换您的 @ 987654331@ 到 IList 并调用非泛型 Add 方法。

public class TaskCollection : IList<Task>, IList
{
    int IList.Add(object value)
    {
        // just call the generic version
        Add((Task)value);
    }

    int IList.IndexOf(object value)
    {
        // just call the generic version
        return IndexOf((Task)value);
    }

    ...
}

【讨论】:

  • 这可能不会成为问题,但有没有强制 T 成为任务?
  • @DominicKexel 如果我实现 IList 我必须实现一堆使用 T 作为泛型类型的方法。我不希望任何使用 TaskCollection 的人使用任务以外的任何其他类型。很有可能我不太了解实现泛型类型接口。
  • @Ukemi 不,如果你实现IList,你必须实现一堆不使用Task,而是object 的方法。但我在这里看到了你的问题。查看我的更新。
猜你喜欢
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 2010-11-12
  • 2011-04-04
  • 1970-01-01
  • 2016-11-02
  • 2012-08-31
  • 1970-01-01
相关资源
最近更新 更多