csharp 表达树穗
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 表达树穗相关的知识,希望对你有一定的参考价值。
// C#, ExpressionTrees
using System;
using System.Linq.Expressions;
namespace Dummy
{
class Program
{
public static void Main(string[] args)
{
//Lambda Expression
Func<int, bool> test = i => i > 5;
Console.WriteLine("Lambda Expression:");
Console.WriteLine(test(2));
Console.WriteLine(test(6));
//C# Compiler (CSC.exe) turns the code above to this: (Microsoft Intermediate Language - MSIL)
Func<int, bool> test2 = GeneratedFuncName;
Console.WriteLine("\nAfter CSC handles:");
Console.WriteLine(test2(2));
Console.WriteLine(test2(6));
//If we wrap it with Expression, CSC does not create a delegate, converts lamda expression to bunch of objects instead.
Expression<Func<int, bool>> exp = i => i > 5;
// *** What CSC did:
ParameterExpression expression3;
Expression<Func<int, bool>> expression =
Expression.Lambda<Func<int, bool>>(
Expression.GreaterThan(expression3 = Expression.Parameter(typeof(int), "i"),
Expression.Constant(5, typeof(int))), new ParameterExpression[] { expression3 });
//Let's look up to the details:
//Expression returns a value. "i > 5" contains 3 expressions, 5, i return the value themselves, and > takes two arguments, returns a bool value.
//Statement returns nothing. Like if, for, while.
//Constant Expressions:
// For the right side of the expression, 5.
ConstantExpression constantExpression = Expression.Constant(5, typeof(int));
Console.WriteLine("\nConstant Expression:");
Console.WriteLine(constantExpression.NodeType);
Console.WriteLine(constantExpression.Type);
Console.WriteLine(constantExpression.Value);
//Parameter Expressions:
//For the left side of the expression, i.
ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "i");
Console.WriteLine("\nParameter Expression:");
Console.WriteLine(parameterExpression.NodeType);
Console.WriteLine(parameterExpression.Type);
Console.WriteLine(parameterExpression.Name);
//Binary Expressions:
//Can be >,<,>= and etc. So in this case, our Binary Expression is GreaterThanExpression.
//For the >.
BinaryExpression binaryExpression = Expression.GreaterThan(parameterExpression, constantExpression);
Console.WriteLine("\nBinary Expression:");
Console.WriteLine(binaryExpression.NodeType);
Console.WriteLine(binaryExpression.Type);
Console.WriteLine(binaryExpression.Left);
Console.WriteLine(binaryExpression.Right);
//Lambda Expression:
//Takes the expression body, which is greater than. And the parameter.
Expression<Func<int, bool>> finalExpression = Expression.Lambda<Func<int, bool>>(binaryExpression, parameterExpression);
Func<int, bool> myDelegate = finalExpression.Compile(); //We directly send our code to JIT (Just in time compiler), and passed CSC.
Console.WriteLine("\nManually created Lambda Expression:");
Console.WriteLine(myDelegate(3));
Console.WriteLine(myDelegate(6));
//IQueryable takes Expression as a paremeter for its Where and Select extensions.
//IEnumerable takes direct delegate.
//This brings us to think that, you can lazy load on IQueryable when getting data from db, viceversa for the IEnumerable.
Console.ReadKey();
}
static bool GeneratedFuncName(int i)
{
return i > 5;
}
}
}
以上是关于csharp 表达树穗的主要内容,如果未能解决你的问题,请参考以下文章