考虑以下代码片段:通用扩展方法返回的IEnumerable <T>不使用反射
public static class MatchCollectionExtensions
{
public static IEnumerable<T> AsEnumerable<T>(this MatchCollection mc)
{
return new T[mc.Count];
}
}
这个类:
public class Ingredient
{
public String Name { get; set; }
}
有什么办法神奇地变换MatchCollection
对象的集合Ingredient
?用例将是这个样子:
var matches = new Regex("([a-z])+,?").Matches("tomato,potato,carrot");
var ingredients = matches.AsEnumerable<Ingredient>();
更新
纯LINQ基础的解决方案就足够了为好。
选项#2工作。谢谢 :) – roosteronacid 2010-04-16 08:35:57