ExpressionReflect 9.0.0

dotnet add package ExpressionReflect --version 9.0.0                
NuGet\Install-Package ExpressionReflect -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ExpressionReflect" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExpressionReflect --version 9.0.0                
#r "nuget: ExpressionReflect, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ExpressionReflect as a Cake Addin
#addin nuget:?package=ExpressionReflect&version=9.0.0

// Install ExpressionReflect as a Cake Tool
#tool nuget:?package=ExpressionReflect&version=9.0.0                

ExpressionReflect

Why?

Provides the ability to "compile" expressions to delegates without using Reflection.Emit but only using reflection. The created delegate will make use of reflection to evaluate the expression when it is invoked. This is very slow when compared to executing a compiled delegate of an expression!

This framework is intended to be used where dynamic code creation is not possible. The main purpose is the usage with Xamarin.iOS due to it's restriction on Reflection.Emit.

How?

The expression tree is traversed and respective reflection calls are created and invoked. This is very slow compared to compiled expressions, so it should only be used with simple expressions.

Usage

The usage is fairly simple. It's just an extension method on a expression.

Expression<Func<Customer, string>> expression = x => x.Firstname;
Func<Customer, string> reflection = expression.Reflect();
string result = reflection.Invoke(customer);

The extension method Reflect() will return a delegate that will execute the reflection-based expression evaluation internally.

What is supported?

The following built-in delegates are supported at the moment:

  • Func<T, TResult> (all variations)
  • Action und Action<T> (all variations)
  • Predicate<T>

The following expression types are supported at the moment:

  • Property getter
x => x.Firstname
  • Property getter with subsequent method call
x => x.Firstname.ToLower();
  • Method call with return value
x => x.CalculateAge()
  • Method call with return value and subsequent method call
x => x.ToString().ToLower();
  • Method call with return value and expression parameters
x => x.CalculateLength(x.Firstname);
  • Method call with return value, expression parameters and binary expression
x => x.Calculate(x.Age + x.Value);
  • Method call with return value, expression parameters, binary expression and constant
x => x.Calculate(x.Age + 100);
  • Method call with return value, expression parameters, binary expression and local variable
int value = 666;
x => x.Calculate(value);
  • Method call with return value, expression parameters and nested constructor call.
int value = 666;
x => x.Calculate(new Customer(value));
  • Method call with return value, expression parameters and nested method call.
x.Calculate(x.CalculateAge());
  • Method call with return value, expression parameters and local delegate call.
Func<int> method = () => 100;
x => x.Calculate(method());
  • Method call with return value, expression parameters and local delegate call with parameters.
Func<int, int> method = x => x + 100;
x => x.Calculate(method(10));
  • Method call with return value and mixed parameters
x => x.CalculateLength(x.Firstname, x, 10);
  • Constructor call
x => new Customer();
  • Constructor call with subsequent method call
x => new Customer();
  • Constructor call with expression parameters
x => new Customer(x.Lastname, x.Firstname);
  • Constructor call with expression parameters and binary expression
x => new Customer(x.Age + x.Value);
  • Constructor call with expression parameters, binary expression and constant
x => new Customer(x.Age + 100);
  • Constructor call with expression parameters, binary expression and local variable
int value = 666;
x => new Customer(value);
  • Constructor call with expression parameters and nested costructor call
int value = 666;
x => new Customer(new Customer(value));
  • Constructor call with expression parameters and nested method call.
x => new Customer(x.CalculateAge());
  • Constructor call with expression parameters and local delegate call.
Func<int> method = () => 100;
x => new Customer(method());
  • Constructor call with expression parameters and local delegate call with parameters.
Func<int, int> method = x => x + 100;
x => new Customer(method(10));
  • Constructor call with mixed parameters
x => new Customer(x.Lastname, x, 10, x.Firstname);

Supported features

  • Func<T, TResult>
  • Action und Action<T>
  • Predicate<T>
  • Property getters including indexers
  • Field access
  • Method calls with mixed parameters
  • Constructor invocations with mixed parameters
  • Local variables
  • Constant expressions
  • Local delegates
  • Local delegates with parameters (local and constant, binary expression)
  • Binary expressions including 'is' operator
  • Unary expressions (almost complete)
  • Array access
  • Array creation (simple arrays)
  • Object initializers (objects, lists, dictionaries)
  • Ternary conditionals
  • Nested expressions (f.e. LINQ extension methods: x => x.FirstOrDefault(y.Name == "John"))

All this features can be combined to more complex expressions.

Contributors

Bernhard Richter seesharper

Thank you!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 91 11/14/2024
4.1.2 84 11/1/2024
4.1.1 102 7/9/2024
4.1.0 246 11/24/2023
4.0.0 121 11/16/2023
3.1.0 317 1/18/2023
3.0.0 346 11/19/2022
2.0.5 444 6/3/2022
2.0.4 481 4/14/2022
2.0.3 459 4/12/2022
2.0.2 435 4/12/2022
2.0.0 362 11/4/2021
1.1.0 1,673 11/20/2014