Getting started with Expression Tree

Source: Internet
Author: User
System. LINQ. Expressions namespace

The system. LINQ. Expressions namespace contains classes, interfaces, and enumeration.CodeThe expression can be represented as an object in the expression directory tree form.

Abstract class expression provides the root directory of the class hierarchy used for modeling the expression directory tree.

Classes (such as memberexpression and parameterexpression) in this namespace derived from expression are used to represent nodes in the expression directory tree. The expression class contains the static (shared in Visual Basic) Factory method. You can create expression directory tree nodes of various types.

The enumerated type expressiontype (ADD, And, call, and so on indicate the action type of the expression) specifies the unique node type.

Common classes

  Binaryexpression The expression that contains binary operators.
  Conditionalexpression An expression that contains conditional operators.
  Constantexpression Indicates an expression with a constant value.
Abstract Expression Provides a base class, indicating that the class of the Expression Tree node is derived from this base class. It also contains the static (shared in Visual Basic) Factory method used to create various node types
  Memberexpression Indicates the access field or attribute.
  Methodcallexpression Call a static method or instance method.
  Newexpression Constructor call.
  Unaryexpression The expression that contains the unary operator.
  Parameterexpression Variable, parameter expression
 
3// Constant expression
 
A// Variable or parameter expression
 
! A// One-dimensional Logical non-expression
 
A + B// Binary addition expression
 
Math. Sin ()// Method call expression
 
NewStringbuilder ()// New Expression

 
Public ClassMyexpression
 
{
 
Public String_ STR;
Public Int_ Int;
 
Public StringName {Get; set ;}
 
PublicMyexpression (){}
 
PublicMyexpression (StringStr ){
 
_ STR = STR;
 
}
 
/// <Summary>
 
/// Create a + 2 + 3 expression.
 
/// Decomposition action. (A + 2) + 3. ==> (variable binary operator + constant) binary operator + constant.
 
/// </Summary>
 
/// <Returns> </returns>
Public StaticFunc <Int,Int> Expression1 ()
 
{
 
Parameterexpression Param = system. LINQ. Expressions. expression. parameter (Typeof(System. int32 ),"");
 
//
 
Binaryexpression plus1 = expression. makebinary (expressiontype. Add, Param,
 
Expression. Constant (2 ));
 
Binaryexpression plus2 = expression. makebinary (expressiontype. Add, plus1,
 
Expression. Constant (3 ));
 
// Or
// Constantexpression con1 = expression. Constant (2 );
 
// Constantexpression con2 = expression. Constant (3 );
 
// Binaryexpression plus1 = expression. Add (Param, con1 );
 
// Binaryexpression plus2 = expression. Add (plus1, con2 );
 
// Compile to delegate
 
Expression <func <Int,Int> Lam = expression. Lambda <func <Int,Int>>( Plus2, Param );
 
ReturnLam. Compile ();;
 
}
Public Static IntTodouble (IntNumber ){
 
ReturnNumber * number;
 
}
 
Public IntTodouble_instance (IntNumber)
 
{
 
ReturnNumber * number;
 
}
 
/// <Summary>
 
/// Call the static method myexpression. todouble
 
/// </Summary>
 
/// <Returns> </returns>
Public StaticFunc <Int,Int> Expression2 (){
 
// Create parameters
 
Parameterexpression Param = expression. parameter (Typeof(Int),"Number");
 
// Call Method
 
Methodcallexpression method = expression. Call (Typeof(Myexpression). getmethod ("Todouble"), Param );
 
ReturnExpression. Lambda <func <Int,Int>>( Method, Param). Compile ();;
 
}
/// <Summary>
 
/// Call the dynamic method myexpression. todouble
 
/// </Summary>
 
/// <Returns> </returns>
 
Public StaticFunc <Int,Int> Expression3 ()
 
{
 
// Create parameters
 
Parameterexpression Param = expression. parameter (Typeof(Int),"Number");
 
// Call Method
Methodcallexpression method = expression. Call (Typeof(Myexpression). getmethod ("Todouble_instance"), Param );
 
ReturnExpression. Lambda <func <Int,Int>>( Method, Param). Compile ();;
 
}
 
/// <Summary>
 
/// Instantiate
 
/// </Summary>
 
/// <Returns> </returns>
Public StaticFunc <String, T> createinstance <t> ()WhereT:Class{
 
// Create parameters
 
Parameterexpression Param = expression. parameter (Typeof(String),"Str");
 
// Call Method
 
VaR exp = expression. New (Typeof(T). getconstructor (NewType [] {Typeof(String)}), Param );
 
ReturnExpression. Lambda <func <String, T> (exp, Param). Compile ();
}
 
/// <Summary>
 
/// Create a new int [] {A, B, A + B} Array
 
/// </Summary>
 
/// <Returns> </returns>
 
Public StaticFunc <Int,Int,Int[]> Createintarray ()
 
{
 
Parameterexpression A = expression. parameter (Typeof(Int),"");
Parameterexpression B = expression. parameter (Typeof(Int),"B");
 
Binaryexpression aplusb = expression. makebinary (expressiontype. Add, A, B );
 
VaR exp = expression. newarrayinit (Typeof(Int), A, B, aplusb );
 
 
 
Parameterexpression [] parm = {a, B };
 
ReturnExpression. Lambda <func <Int,Int,Int[]> (Exp, parm). Compile ();
 
}
 
/// <Summary>
/// A. length> 3 | B> 0
 
/// </Summary>
 
/// <Returns> </returns>
 
Public StaticFunc <String,Int,Bool> Createbool (){
 
Parameterexpression A = expression. parameter (Typeof(String),"");
 
VaR property = expression. propertyorfield (,"Length");
 
VaR exp1 = expression. makebinary (expressiontype. greaterthan, property, expression. Constant (3 ));
 
 
Parameterexpression B = expression. parameter (Typeof(Int),"B");
 
VaR exp2 = expression. makebinary (expressiontype. greaterthan, B, expression. Constant (0 ));
 
 
 
VaR OR = expression. makebinary (expressiontype. Or, exp1, exp2 );
 
 
 
ReturnExpression. Lambda <func <String,Int,Bool>>( Or, a, B). Compile ();
 
}
 
 
 
Public StaticIqueryable <t> order <t> (iqueryable <t> query,StringField)
{
 
Parameterexpression x = expression. parameter (Typeof(T ),"X");
 
VaR property = expression. Property (x, field );
 
 
 
Propertyinfo Pi =Typeof(T). getproperty (field );
 
Type [] types =NewType [2];
 
Types [0] =Typeof(T );
 
Types [1] = pi. propertytype;
 
VaR Lambda = expression. Lambda (property, X );
 
VaR exp = query. asqueryable (). Expression;
Methodcallexpression call = expression. Call (Typeof(Queryable ),"Orderby", Types, exp, lambda );
 
ReturnQuery. asqueryable (). provider. createquery <t> (CALL );
 
 
 
}
 
/// <Summary>
 
/// Instantiate 2
 
/// </Summary>
 
/// <Returns> </returns>
 
Public StaticFunc <Object> Createinstance (StringClasspath)
 
{
// Call Method
 
VaR exp = expression. New (type. GetType (classpath). getconstructor (type. emptytypes ));
 
ReturnExpression. Lambda <func <Object>>( Exp). Compile ();
 
}
 
}
Note that the original expression needs to be added for the last call method of the dynamic query of LINQ, which is not understood for the time being.
 
Static VoidMain (String[] ARGs)
 
{
Expression <func <Int,Int,Int> Expression = (a, B) => A + B;
 
 
 
VaR per = myexpression. expression2 ();
 
Console. writeline ("Call static methods :"+ Per (5 ));
 
Console. writeline ("Call the dynamic method :"+ Myexpression. expression2 () (10 ));
 
 
 
Myexpression exp = myexpression. createinstance <myexpression> ()("Jianjialin");
 
Console. writeline ("Create object :"+ Exp. _ STR +""+ Myexpression. createinstance <myexpression> (). tostring ());
 
 
Console. writeline ("Create an array :"+ Myexpression. createintarray () (2, 3) [2]);
 
 
 
Console. writeline ("A. length> 3 | B> 0 Expression"+ Myexpression. createbool ()("ABCD",-2 ));
 
 
 
Ilist <student> lst =NewList <student> {
 
NewStudent {name ="Liu Bei", Age = 40 },
 
NewStudent {name ="Zhang Fei", Age = 20 },
 
NewStudent {name ="Guan Yu", Age = 30}
 
 
 
};
Console. readkey ();
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.