c# – 玩匿名类型
从Jon Skeet的精彩书C#深度,第一版:
class Film { public string Name { get; set; } public int Year { get; set; } public override string ToString() { return string.Format("Name={0},Year={1}",Name,Year); } } var films = new List<Film> { new Film {Name="Jaws",Year=1975},new Film {Name="Singing in the Rain",Year=1952},new Film {Name="Some Like It Hot",Year=1959},new Film {Name="The Wizard of Oz",Year=1939},new Film {Name="It's a Wonderful Life",Year=1946},new Film {Name="American Beauty",Year=1999},new Film {Name="High Fidelity",Year=2000},new Film {Name="The Usual Suspects",Year=1995} }; Action<Film> print = film => { Console.WriteLine(film); }; films.ForEach(print); films.FindAll(film => film.Year < 1960) .ForEach(print); films.Sort((f1,f2) => f1.Name.CompareTo(f2.Name)); films.ForEach(print); 一段落在上面列出的代码段之后.
而上面提供的代码片段,是该段所指的书的9.4. 我的问题: 我试过这样的东西,但这不是他的意思,我想,因为它不工作(我没想到) using System; using System.Collections.Generic; namespace ScratchPad { class Film { public string Name { get; set; } public int Year { get; set; } public override string ToString() { return string.Format("Name = {0}tYear = {1}",Year); } } class Program { static void Main(string[] args) { ToList<Film>( new[] { new { Name = "North By Northwest",Year = 1959 },new { Name = "The Green Mile",Year = 1999},new { Name = "The Pursuit of Happyness",Year = 2006} }).ForEach( f => {Console.WriteLine(f);} ); Console.ReadKey(); } static List<T> ToList<T>( System.Collections.IEnumerable list) { var newList = new List<T>(); foreach (var thing in list) if (thing is T) newList.Add((T)thing); return newList; } } } 注意:我知道IEnumerable.ToList()扩展方法,并且已经使用了很多次.我只想手工尝试该段落中概述的技术. 此外,我对匿名类型在Linq之外使用的场景感兴趣,作为句法方便,下面给出了一种这样的场景.我可以随时在C#4中使用动态,并接受一个匿名类型作为参数,并与它一起工作,了解我的期望.我希望我可以用C#3做到这一点.如下所示: using System; using Microsoft.CSharp.RuntimeBinder; namespace PlayWithAnonType { class Program { static void Main(string[] args) { PrintThingy(new { Name = "The Secret",Genre = "Documentary",Year = 2006 }); Console.ReadKey(); } static void PrintWhatever(dynamic whatever) { // the anonymous type's ToString() will print Console.WriteLine(whatever); } static void PrintThingy(dynamic thingy) { try { // I know what the thingy is Console.WriteLine("Name = {0}tGenre = {1}tYear = {2}",thingy.Name,thingy.Genre,thingy.Year); } catch(RuntimeBinderException ex) { #pragma warning disable 0168 Console.WriteLine("By thingy,I really meant film. Sorry,I should've clarified."); #pragma warning restore 0168 } } } } 编辑 解决方法
要做到这一点:
public void Main (string[] args) { var films = ToList(new [] { new {Name = "Jaws",Year = 1975},new {Name = "Singing in the Rain",Year = 1952},new {Name = "Some Like It Hot",Year = 1959},new {Name = "The Wizard of Oz",Year = 1939},new {Name = "It's a Wonderful Life",Year = 1946},new {Name = "American Beauty",new {Name = "High Fidelity",Year = 2000},new {Name = "The Usual Suspects",Year = 1995} } ); films.ForEach(f => Console.Write(f.Name + " - " + f.Year)); } public List<T> ToList<T> (IEnumerable<T> list) { return new List<T>(list); } 正如其他人所说,我不知道这是多么有用.你会得到智能感知和所有这些,当你写,所以可能有一些打字节省,至少? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |