
The concept was inspired by Pascal Ganaye's demo for his expression evaluator. There is also a demo application that shows a practical usage of the library.

Compare this with interpretive evaluators that have to go through several function (and possibly Reflection) calls and if statements to evaluate the same expression. The IL stream above is only 35 bytes long, and will be JITed to native processor instructions. Note how the a and b fields, which are integers, were implicitly converted to doubles for the ^ operator, and how the ^ operator was translated to a call to the Pow function. L_001d: call float64 System.Math::Sqrt(float64) L_000f: ldfld int32 ConsoleTester.ExpressionOwner::b L_0001: ldfld int32 ConsoleTester.ExpressionOwner::a We will declare a class and give it two fields to represent the a and b sides of the triangle: Once attached, the dynamic method will behave as a static function on the class and will have access to all of its members (even non-public ones). This is a class to which the dynamic method generated by the expression will be attached. First, we need to declare our expression owner. To start, we will need to add a reference to the and import the ciloci.Flee namespace. Walkthrough: Creating and Evaluating an Expressionįor this walkthrough, we are going to compute the hypotenuse of a triangle with sides a and b, using the expression sqrt(a^2 + b^2). Essentially, it does the same thing as a C# compiler except that it compiles at runtime, emits its IL to memory instead of an assembly, and is designed to work with expressions (which are a subset of a complete programming language). L_000f: callvirt instance string System.Object::ToString()Īutomating the translation of expressions to IL is where this library comes in.
Property evaluator for windows code#
For example, here is the IL required to evaluate the expression ():Ĭopy Code L_0001: call valuetype System.DateTime System.DateTime::get_Now() Since IL is basically a form of assembly language, performing even simple tasks requires lots of instructions and an understanding of the low-level workings of the CLR. One of its weaknesses is that the only way to create the method body is to emit IL. Its main advantages are that the generated method and its code can be garbage collected when no longer referenced, and it does not generate any dynamic assemblies that stay in memory. Using its DynamicMethod class, you are able to create a method at runtime, set the code of the method body, and finally call the created method. NET 2.0 to allow for efficient runtime code generation. Lightweight CodeGen (LCG) is a feature introduced in. Allows for customization of expression compilation.Emits short-circuited logical operations.Supports 32/64 bit, signed/unsigned, and hex integer literals.Supports string, char, boolean, and floating-point literals.Supports all arithmetic operations including the power (^) operator.Does not create any dynamic assemblies that stay in memory.

