【问题标题】:List<T> of Delegates in a Class that is IEnumerableIEnumerable 类中的委托列表<T>
【发布时间】:2011-07-08 00:49:16
【问题描述】:

我有一个用户定义的类,我想创建一个公共列表作为其中的一部分。我希望 List 是一个委托函数列表,我可以添加这些委托函数并将每个列表成员设置为一个委托函数。我希望这个函数列表成为我实例化的类的一部分,所以当我将它传递给其他函数时,它会跟随类的实例。我需要能够通过 foreach 循环调用委托函数,因此它也必须是 IEnumberable。

我已经尝试了几个小时,我所拥有的可能会或可能不会做部分工作。当它开始看起来我需要为自定义列表编写我自己的 IEnumberation 例程时,我意识到我有点不知所措并来到了这里。

这是我的代码:

    public delegate List<ChartTestModel> MyDelegate<T>(T i);
    public class DelegateList<T> 
    {
        public void Add(MyDelegate<T> del)
        {
            imp.Add(del);
        }

        public void CallDelegates(T k)
        {
            foreach (MyDelegate<T> del in imp)
            {
                del(k);
            }
        }
        private List<MyDelegate<T>> imp = new List<MyDelegate<T>>();

    }

我什至不知道这是否符合我的要求。不过,我知道我无法通过 ForEach。它完全是由在 Google 上查找的拼凑代码编写的。我几乎不明白它应该做什么。

【问题讨论】:

标签: c# class list delegates ienumerable


【解决方案1】:

我完全不明白您为什么需要自定义类。只需使用List&lt;T&gt;,其中 T 是任何委托类型。

List<Action> actions = new List<Action>();
actions.Add(() => blah blah);
actions.Add(Whatever); // Whatever() is a method

// now run them all
actions.ForEach(a => a());

【讨论】:

  • 我如何让这部分成为我的课堂?我尝试将 List 行作为公共列表放入,但它没有显示为类的属性供我交互。
【解决方案2】:

IEnumerable&lt;T&gt; 实现起来很简单,特别是当你有一个集合作为类的成员时。您需要做的就是定义适当的GetEnumerator 方法,最简单的方法是返回底层集合的枚举数。

class YourClass : IEnumerable<SomeClass>
{
    List<SomeClass> list = ...

    public IEnumerator<SomeClass> GetEnumerator() 
    {
        return list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator(); 
    }
}

在这里,您为IEnumerable&lt;T&gt; 隐式实现方法,为IEnumerable 显式实现方法。 (你必须同时实现IEnumerable&lt;T&gt;继承IEnumerable。)

对于您的特定课程,您可能有

public class DelegateList<T> : IEnumerable<MyDelegate<T>>
{
    // ...other class details 

    private List<MyDelegate<T>> imp = new List<MyDelegate<T>>();

    public IEnumerator<MyDelegate<T>> GetEnumerator()
    {
        return imp.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

【讨论】:

  • 无法将类型 'System.Collections.Generic.List>.Enumerator' 隐式转换为 'System.Collections.Generic.IEnumerator' 使用泛型类型 'System.Collections.Generic.IEnumerable' 需要 1 个类型参数 使用泛型类型 'System.Collections.Generic.IEnumerator' 需要 1 个类型参数
  • @user583281:将using System.Collections; 添加到文件中。
  • 啊,我有 System.Collections.Generic,但没有它的父级。
【解决方案3】:

我希望这对你有用。

    static void Main(string[] args)
    {
        var delegateFuncs = new List<Func<string , string>> { 
            l1=>{ return "at1:" + l1;} , 
            l2=>{ return "at2:" + l2;} , 
            l3=>{ return "at3:" + l3;} , 
            l4=>{ return "at4:" + l4;} 
        };

        string parameter = "test";

        foreach (var f in delegateFuncs)
        {
            Console.WriteLine(f(parameter));    
        }

        Console.ReadLine();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2017-07-02
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多