Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created June 8, 2012 05:12
Show Gist options
  • Save masaru-b-cl/2893718 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/2893718 to your computer and use it in GitHub Desktop.
IEnumerable<Action<T>>の全実行拡張メソッド
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