iniquity
iniquity

Reputation: 15

List<T> of Delegates in a Class that is IEnumerable

I have a user defined class that I want to create a public List as part of. I want the List to be a List of delegate functions that I can add to and set each List Member to a delegate function. I want this list of functions to be part of the class I instantiate, so it follows the instance of the class as I pass it to other functions. I need the ability to call the delegated functions via a foreach loop, so it also has to be IEnumberable.

I've been trying for several hours, what I have may or may not do part of the job. When it started looking like I needed to write my own IEnumberation routines for the custom List, I realize I was in way over my head and came here.

This is the code I have:

    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>>();

    }

I don't even know if this does what I want it to or not. I know I can't ForEach through it, though. It's written entirely from pieced together code from looking on Google. I barely understand what it's supposed to do.

Upvotes: 1

Views: 1192

Answers (3)

shenhengbin
shenhengbin

Reputation: 4294

I hope this is userful to you.

    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();
    }

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126932

IEnumerable<T> is simple to implement, particularly when you have a collection as a member of the class. All you need to do is define appropriate GetEnumerator methods, and the easiest thing to do is return the enumerator of the underlying collection.

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

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

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

Here, you implement methods for implicitly for IEnumerable<T> and explicitly for IEnumerable. (You have to implement both as IEnumerable<T> inherits IEnumerable.)

For your specific class, you might have

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();
    }
}

Upvotes: 1

Samuel Neff
Samuel Neff

Reputation: 74929

I don't see why you need a custom class at all. Just use List<T> where T is whatever delegate type.

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());

Upvotes: 2

Related Questions