Created
June 8, 2012 05:12
-
-
Save masaru-b-cl/2893718 to your computer and use it in GitHub Desktop.
IEnumerable<Action<T>>の全実行拡張メソッド
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
public static class EnumerableExtensions | |
{ | |
public static void Invoke<T>(this IEnumerable<Action<T>> actions, T value) | |
{ | |
foreach (var action in actions) | |
{ | |
action(value); | |
} | |
} | |
} | |
class Program | |
{ | |
private static void Hoge(string value) | |
{ | |
Console.WriteLine("Hoge " + value); | |
} | |
private static void Foo(string value) | |
{ | |
Console.WriteLine("Foo " + value); | |
} | |
private static void Bar(string value) | |
{ | |
Console.WriteLine("Bar " + value); | |
} | |
static void Main(string[] args) | |
{ | |
var actions = new Action<string>[] { Hoge, Foo, Bar }; | |
// Combineするくらいなら | |
var action = (Action<string>)Delegate.Combine(actions); | |
action("Sho"); | |
// 拡張メソッド作った方が分かりやすい | |
actions.Invoke("Sho"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment