How can I write a generic container class that implements a given interface in C#?

后端 未结 7 1751
太阳男子
太阳男子 2020-12-02 16:17

Context: .NET 3.5, VS2008. I\'m not sure about the title of this question, so feel free to comment about the title, too :-)

Here\'s the scenario: I have several cla

7条回答
  •  囚心锁ツ
    2020-12-02 16:36

    It's not as clean an interface as the reflection based solution, but a very simple and flexible solution is to create a ForAll method like so:

    static void ForAll(this IEnumerable items, Action action)
    {
        foreach (T item in items)
        {
            action(item);
        }
    }
    

    And can be called like so:

    arr.ForAll(x => x.Start());
    

提交回复
热议问题