C#学习日记 表达式树

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

namespace Sample
{
	public class MainEntryPoint
	{
		static int Main(string[] args)
		{
			Expression<Func<Racer, bool>> expression = r => r.Country == "UK" && r.Wins > 6;
			DisplayTree(0, "lambda", expression);
			return 0;
		}
		private static void DisplayTree(int indent, string message, Expression expression)
		{
			string output = string.Format("{0} {1}!NodeType:{2};Expr:{3}","".PadLeft(indent, '>'), message, expression.NodeType, expression);
			indent++;
			switch(expression.NodeType)
			{
				case ExpressionType.Lambda:
					Console.WriteLine(output);
					LambdaExpression lambdaExpr = (LambdaExpression)expression;
					foreach(var parameter in lambdaExpr.Parameters)
					{
						DisplayTree(indent, "Paramter", parameter);
					}
					DisplayTree(indent, "Body", lambdaExpr.Body);
					break;
				case ExpressionType.Parameter:
					ParameterExpression parameterExpr = (ParameterExpression)expression;
					Console.WriteLine("{0} Parameter Type:{1}", output, parameterExpr.Type.Name);
					break;
				case ExpressionType.Constant:
					ConstantExpression constExpr = (ConstantExpression)expression;
					Console.WriteLine("{0} Constant Value:{1}", output, constExpr.Value);
					break;
				case ExpressionType.Equal:
				case ExpressionType.AndAlso:
				case ExpressionType.GreaterThan:
					BinaryExpression binExpr = (BinaryExpression)expression;
					if(binExpr.Method != null)
					{
						Console.WriteLine("{0} Method:{1}", output, binExpr.Method.Name);
					}
					else
					{
						Console.WriteLine(output);
					}
					DisplayTree(indent, "Left", binExpr.Left);
					DisplayTree(indent, "Right", binExpr.Right);
					break;
				case ExpressionType.MemberAccess:
					MemberExpression memberExpr = (MemberExpression)expression;
					Console.WriteLine("{0} Member Name:{1} Type:{2}", output, memberExpr.Member.Name, memberExpr.Type.Name);
					DisplayTree(indent, "Member Expr", memberExpr.Expression);
					break;
				default :
					Console.WriteLine();
					Console.WriteLine("{0} {1}",expression.NodeType, expression.Type.Name);
					break;
			}
		}
	}
	[Serializable]
	public class Racer : IComparable<Racer>, IFormattable
	{
		public string FirstName { get; set;}
		public string LastName { get; set;}
		public int Wins { get; set;}
		public string Country { get; set;}
		public int Starts { get; set;}
		public IEnumerable<string> Cars { get; private set;}
		public IEnumerable<int> Years { get; private set;}

		public Racer(string firstName, string lastName, int wins, string country, int starts, IEnumerable<string> cars,IEnumerable<int> years)
		{
			this.FirstName = firstName;
			this.LastName = lastName;
			this.Wins = wins;
			this.Country = country;
			this.Starts = starts;
			this.Cars = new List<string>(cars);
			this.Years =new List<int>(years);
		}
		public Racer(string firstName, string lastName, int wins, string country, int starts): this(firstName, lastName, wins, country, starts, null, null)
		{
		}
		public override string ToString()
		{
			return string.Format("{0} {1}", FirstName, LastName);
		}
		public int CompareTo(Racer other)
		{
			if(other == null)
				return -1;
			return string.Compare(this.LastName, other.LastName);
		}
		public string ToString(string format)
		{
			return ToString(format, null);
		}
		public string ToString(string format, IFormatProvider formatProvider)
		{
			switch(format)
			{
				case null:
				case "N":
					return ToString();
				case "F":
					return FirstName;
				case "L":
					return LastName;
				case "C":
					return Country;
				case "S":
					return Starts.ToString();
				case "W":
					return Wins.ToString();
				case "A":
					return string.Format("{0} {1}, {2}, starts: {3}, wins: {4}", FirstName, LastName, Country, Starts, Wins);
				default:
					throw new FormatException(string.Format("Format {0} not supported", format));
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值