C#学习日记 表达式树

    xiaoxiao2021-03-25  112

    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)); } } } }
    转载请注明原文地址: https://ju.6miu.com/read-12116.html

    最新回复(0)