Briefly introduce the sample code of c# expression tree Expression simple type comparison demo

黄舟
Release: 2017-03-22 11:53:47
Original
1408 people have browsed it

The following editor will bring you a brief discussionc#ExpressionTree Expression simple type comparison demo. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

The examples are as follows:

using System; using System.Linq.Expressions; class DynamicPredicate { public static Expression> Generate(string op) { ParameterExpression x = Expression.Parameter(typeof(T), "x"); ParameterExpression y = Expression.Parameter(typeof(T), "y"); return Expression.Lambda> ( (op.Equals(">")) ? Expression.GreaterThan(x, y) : (op.Equals("<")) ? Expression.LessThan(x, y) : (op.Equals(">=")) ? Expression.GreaterThanOrEqual(x, y) : (op.Equals("<=")) ? Expression.LessThanOrEqual(x, y) : (op.Equals("!=")) ? Expression.NotEqual(x, y) : Expression.Equal(x, y), x, y ); } } static void Main() { string op = ">="; var integerPredicate = DynamicPredicate.Generate(op).Compile(); var floatPredicate = DynamicPredicate.Generate(op).Compile(); int iA = 12, iB = 4; Console.WriteLine("{0} {1} {2} : {3}", iA, op, iB, integerPredicate(iA, iB)); float fA = 867.0f, fB = 867.0f; Console.WriteLine("{0} {1} {2} : {3}", fA, op, fB, floatPredicate(fA, fB)); Console.WriteLine("{0} {1} {2} : {3}", fA, ">", fB, DynamicPredicate.Generate(">").Compile()(fA, fB)); Console.ReadLine(); }
Copy after login

The above is the detailed content of Briefly introduce the sample code of c# expression tree Expression simple type comparison demo. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!