Generic extension method returning IEnumerable<T> without using reflection
- by roosteronacid
Consider this snippet of code:
public static class MatchCollectionExtensions
{
public static IEnumerable<T> AsEnumerable<T>(this MatchCollection mc)
{
return new T[mc.Count];
}
}
And this class:
public class Ingredient
{
public String Name { get; set; }
}
Is there any way to magically transform a MatchCollection object to a collection of Ingredient? The use-case would look something like this:
var matches = new Regex("([a-z])+,?").Matches("tomato,potato,carrot");
var ingredients = matches.AsEnumerable<Ingredient>();