Generic extension method returning IEnumerable<T> without using reflection
Posted
by roosteronacid
on Stack Overflow
See other posts from Stack Overflow
or by roosteronacid
Published on 2010-04-16T08:03:04Z
Indexed on
2010/04/16
8:03 UTC
Read the original article
Hit count: 266
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>();
© Stack Overflow or respective owner