以下を簡素化するために C#/LINQ に組み込まれている機能があるかどうか疑問に思っています。
foreach(var item in collection)
{
if (item.GetType() == typeof(Type1)
DoType1(item as Type1);
else if (item.GetType() == typeof(Type2))
DoType2(item as Type2);
...
}
次の行に沿ったものに:
collection.ForEachType(Type1 item => DoType1(item), Type2 item => DoType2(item));
私は次のことが近いことを認識しています:
collection.OfType<Type1>.ToList().Foreach(item => DoType1(item));
collection.OfType<Type2>.ToList().Foreach(item => DoType2(item));
ただし、コードがコレクションの順序に依存している場合は機能しません。