将Action Delegate,Extensions和Anonymous Type集成在一起的问题

时间:2016-09-07 16:14:24

标签: c# delegates extension-methods anonymous-types func

所以我在C#中使用Extensions,Action和Func试图模仿Ruby处理集合上的Iteration的方式。想法是对象(可枚举)处理自己的迭代。我只使用Extensions和代表取得了成功,我的代码如下所示正在工作;

using System;
using System.Collections.Generic;
using System.Linq;

public class Student{ 
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
}

public static class Extensions {
    //for when you only need some operations on the items in the collection
    public static void ForEach<T>(this IEnumerable<T> items, Action<T> action){

        foreach(var item in items){
            action(item);           
        }
    }

    //for when you want to return a modified collection
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> items, Func<T,T> action){

        foreach(var item in items){
            yield return action(item);          
        }
    }

} 


public class Program
{   
    public static void Main()
    {
        IList<Student> studentList = new List<Student>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 12 },
            new Student() { StudentID = 2, StudentName = "Moin", Age = 13 },
            new Student() { StudentID = 3, StudentName = "Bill", Age =24 },
            new Student() { StudentID = 4, StudentName = "Ram" , Age =22 },
            new Student() { StudentID = 5, StudentName = "Ron"  } 
        };

        var selectResult = studentList.Select(s => s.StudentName);


        selectResult.ForEach(name => Console.WriteLine("Name: {0}", name));

        int i = 1;
        selectResult.ForEach(name => string.Format("Name {0}: {1}", i++, name)).ForEach(formattedName => Console.WriteLine(formattedName));

    }
}

然后我决定将匿名类型添加到混合中并编辑上面的代码,使其变为如下所示,以及我的问题所在。我一直遇到运行时异常,似乎无法弄清问题。请帮助查看或运行它以查看。

错误是; 运行时异常(第17行):尝试方法&#39; DynamicClass.CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,System.Object)&#39;访问类型&#39;&lt;&gt; f__AnonymousType0`2&#39;失败。

using System;
using System.Collections.Generic;
using System.Linq;

public class Student{ 
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
}

public static class Extensions {


    public static void ForEach<T,TInt>(this IEnumerable<dynamic> items, Action<T,TInt> action){

        foreach(var item in items){
            action(item.Name, item.Age);            
        }
    }

} 


public class Program
{   
    public static void Main()
    {
        IList<Student> studentList = new List<Student>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 12 },
            new Student() { StudentID = 2, StudentName = "Moin", Age = 13 },
            new Student() { StudentID = 3, StudentName = "Bill", Age =24 },
            new Student() { StudentID = 4, StudentName = "Ram" , Age =22 },
            new Student() { StudentID = 5, StudentName = "Ron"  } 
        };

        var selectResult = from student in studentList
            select new {Name = student.StudentName, Age = student.Age};


        selectResult.ForEach((string name, int age) => Console.WriteLine("Name: {0}, Age: {1}", name, age));

    }
}

这是在dotnet小提琴上的链接 https://dotnetfiddle.net/ZT7ZB4

0 个答案:

没有答案