c# - Issue integrating Action Delegate, Extensions, and Anonymous Type together -
so playing extensions, action , func in c# in trying mimic way ruby handles iteration on collections. idea objects (that enumerable) handle own iterations. got success extensions , delegates alone , have code below working expected;
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 need operations on items in collection public static void foreach<t>(this ienumerable<t> items, action<t> action){ foreach(var item in items){ action(item); } } //for when want return 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 = 1; selectresult.foreach(name => string.format("name {0}: {1}", i++, name)).foreach(formattedname => console.writeline(formattedname)); } }
then decided throw anonymous types mix , edited above code become below , that's problem is. keep getting run-time exception , can't seem figure out problem. kindly through or run see.
the error is; run-time exception (line 17): attempt method 'dynamicclass.callsite.target(system.runtime.compilerservices.closure, system.runtime.compilerservices.callsite, system.object)' access type '<>f__anonymoustype0`2' failed.
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 = 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)); } }
here's link on dotnet fiddle https://dotnetfiddle.net/zt7zb4
Comments
Post a Comment