表达式树
程序员文章站
2023-02-08 13:31:19
记录表达式树的学习过程 表达式树将代码表示为可以检测、修改、或执行的一种结构,一种定义代码的结构。 表达式树是代码的完整表示形式:可以看到任何子表达式的值。 可以看到方法和属性名称。 可以看到任何常数表达式的值。 还可以将自己转换为可执行的委托,并执行代码。 通过表达式树 API,可创建几乎任何有效 ......
记录的学习过程
表达式树将代码表示为可以检测、修改、或执行的一种结构,一种定义代码的结构。
表达式树是代码的完整表示形式:可以看到任何子表达式的值。 可以看到方法和属性名称。 可以看到任何常数表达式的值。 还可以将自己转换为可执行的委托,并执行代码。
通过表达式树 api,可创建几乎任何有效代码构造的树。 但不能在表达式树中创建某些 c# 习惯用语,第一 异步的async、await,第二是循环(循环有什么习惯用语?我凌乱了)。
表达式树是不可变的数据结构, 只能换新树。
访问表达式树变量
public static void main() { expression<func<int, int>> addfive = num => num + 5; // expressiontype有add、addchecked、and、andalso、divide、equal、lambda、multiply等。 if (addfive.nodetype== expressiontype.lambda) { var lambdaexp = (lambdaexpression)addfive; var parameter = lambdaexp.parameters.first(); console.writeline(parameter.name); console.writeline(parameter.type); console.writeline(lambdaexp.parameters.last().name); //跟上面同一个 console.writeline(lambdaexp.parameters.last().type); } }
创建表达式树
public static void main() { // 常量表达式树 constantexpression one = expression.constant(1, typeof(int)); constantexpression two = expression.constant(2, typeof(int)); // 二进制表达式树 binaryexpression addition = expression.add(one, two); }
表达式树api
expressiontype:(convertchecked 和convert明明c#写法一样,怎么表达不同节点类型,迷茫... multiply和multiplychecked,迷茫...,newarraybounds 多维数组)
// // 摘要: // an addition operation, such as a + b, without overflow checking, for numeric // operands. add = 0, // // 摘要: // an addition operation, such as (a + b), with overflow checking, for numeric operands. addchecked = 1, // // 摘要: // a bitwise or logical and operation, such as (a & b) in c# and (a and b) in visual // basic. and = 2, // // 摘要: // a conditional and operation that evaluates the second operand only if the first // operand evaluates to true. it corresponds to (a && b) in c# and (a andalso b) // in visual basic. andalso = 3, // // 摘要: // an operation that obtains the length of a one-dimensional array, such as array.length. arraylength = 4, // // 摘要: // an indexing operation in a one-dimensional array, such as array[index] in c# // or array(index) in visual basic. arrayindex = 5, // // 摘要: // a method call, such as in the obj.samplemethod() expression. call = 6, // // 摘要: // a node that represents a null coalescing operation, such as (a ?? b) in c# or // if(a, b) in visual basic. coalesce = 7, // // 摘要: // a conditional operation, such as a > b ? a : b in c# or if(a > b, a, b) in visual // basic. conditional = 8, // // 摘要: // a constant value. constant = 9, // // 摘要: // a cast or conversion operation, such as (sampletype)obj in c#or ctype(obj, sampletype) // in visual basic. for a numeric conversion, if the converted value is too large // for the destination type, no exception is thrown. convert = 10, // // 摘要: // a cast or conversion operation, such as (sampletype)obj in c#or ctype(obj, sampletype) // in visual basic. for a numeric conversion, if the converted value does not fit // the destination type, an exception is thrown. convertchecked = 11,
// // 摘要: // a division operation, such as (a / b), for numeric operands. divide = 12, // // 摘要: // a node that represents an equality comparison, such as (a == b) in c# or (a = // b) in visual basic. equal = 13, // // 摘要: // a bitwise or logical xor operation, such as (a ^ b) in c# or (a xor b) in visual // basic. exclusiveor = 14, // // 摘要: // a "greater than" comparison, such as (a > b). greaterthan = 15, // // 摘要: // a "greater than or equal to" comparison, such as (a >= b). greaterthanorequal = 16, // // 摘要: // an operation that invokes a delegate or lambda expression, such as sampledelegate.invoke(). invoke = 17, // // 摘要: // a lambda expression, such as a => a + a in c# or function(a) a + a in visual // basic. lambda = 18, // // 摘要: // a bitwise left-shift operation, such as (a << b). leftshift = 19, // // 摘要: // a "less than" comparison, such as (a < b). lessthan = 20, // // 摘要: // a "less than or equal to" comparison, such as (a <= b). lessthanorequal = 21, // // 摘要: // an operation that creates a new system.collections.ienumerable object and initializes // it from a list of elements, such as new list<sampletype>(){ a, b, c } in c# or // dim samplelist = { a, b, c } in visual basic. listinit = 22, // // 摘要: // an operation that reads from a field or property, such as obj.sampleproperty. memberaccess = 23, // // 摘要: // an operation that creates a new object and initializes one or more of its members, // such as new point { x = 1, y = 2 } in c# or new point with {.x = 1, .y = 2} in // visual basic. memberinit = 24,
// // 摘要: // an arithmetic remainder operation, such as (a % b) in c# or (a mod b) in visual // basic. modulo = 25, // // 摘要: // a multiplication operation, such as (a * b), without overflow checking, for numeric // operands. multiply = 26, // // 摘要: // an multiplication operation, such as (a * b), that has overflow checking, for // numeric operands. multiplychecked = 27, // // 摘要: // an arithmetic negation operation, such as (-a). the object a should not be modified // in place. negate = 28, // // 摘要: // a unary plus operation, such as (+a). the result of a predefined unary plus operation // is the value of the operand, but user-defined implementations might have unusual // results. unaryplus = 29, // // 摘要: // an arithmetic negation operation, such as (-a), that has overflow checking. the // object a should not be modified in place. negatechecked = 30, // // 摘要: // an operation that calls a constructor to create a new object, such as new sampletype(). new = 31, // // 摘要: // an operation that creates a new one-dimensional array and initializes it from // a list of elements, such as new sampletype[]{a, b, c} in c# or new sampletype(){a, // b, c} in visual basic. newarrayinit = 32, // // 摘要: // an operation that creates a new array, in which the bounds for each dimension // are specified, such as new sampletype[dim1, dim2] in c# or new sampletype(dim1, // dim2) in visual basic. newarraybounds = 33, // // 摘要: // a bitwise complement or logical negation operation. in c#, it is equivalent to // (~a) for integral types and to (!a) for boolean values. in visual basic, it is // equivalent to (not a). the object a should not be modified in place. not = 34,
(quote节点类型是什么?)
// // 摘要: // a bitwise complement or logical negation operation. in c#, it is equivalent to // (~a) for integral types and to (!a) for boolean values. in visual basic, it is // equivalent to (not a). the object a should not be modified in place. not = 34, // // 摘要: // an inequality comparison, such as (a != b) in c# or (a <> b) in visual basic. notequal = 35, // // 摘要: // a bitwise or logical or operation, such as (a | b) in c# or (a or b) in visual // basic. or = 36, // // 摘要: // a short-circuiting conditional or operation, such as (a || b) in c# or (a orelse // b) in visual basic. orelse = 37, // // 摘要: // a reference to a parameter or variable that is defined in the context of the // expression. for more information, see system.linq.expressions.parameterexpression. parameter = 38, // // 摘要: // a mathematical operation that raises a number to a power, such as (a ^ b) in // visual basic. power = 39, // // 摘要: // an expression that has a constant value of type system.linq.expressions.expression. // a system.linq.expressions.expressiontype.quote node can contain references to // parameters that are defined in the context of the expression it represents. quote = 40, // // 摘要: // a bitwise right-shift operation, such as (a >> b). rightshift = 41,
expression:binaryexpression、indexexpression、methodcallexpression、unaryexpression、blockexpression、gotoexpression、dynamicexpression、lambdaexpression、memberexpression等。
expressionvisitor。
上一篇: C/C++基础知识面试题回顾及答案分享
下一篇: AI怎么绘制卡通小羊肖恩角色?