Show / Hide Table of Contents

Compound Expressions Construction

Expression class provides builder static methods for constructing expression trees. The construction code loss its readability because builder methods oriented for tree-like representation of the final expression. Metaprogramming library offers set of extension methods aimed to simplification of expression tree construction.

using DotNext.Metaprogramming;

var expr = (-(42.Quoted).Convert<long>()) + 1L.Quoted; // equivalent to -((long)42) + 1L

The following example demonstrates how to construct throw statement:

using System;
using DotNext.Metaprogramming;

typeof(Exception).New("Exception message".Quoted).Throw()    //equivalent to new Exception("Exception message")

Compound expression can be constructed with dynamic type:

using System;
using DotNext.Metaprogramming;

dynamic expr = 42.Quoted.AsDynamic();
expr = -expr + 1;
Expression tree = expr; //tree is -42 + 1

or with overloaded operators:

using System;
using DotNext.Metaprogramming;

Expression expr = 42.Quoted;
expr = -expr + 1.Quoted; //expr is -42 + 1

Checked and unchecked versions of the arithmetic operators are supported.

  • Edit this page
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾