Introduction

Problem

You need to get or set a value from your entity without using a slow technique like reflection.

Solution

The Eval-Expression.NET can easily create optimized getter and setter.

Example

// using Z.Expressions; // Don't forget to include this.

// public class Customer
// {
//     public string Name { get; set; }
// }

var customer = new Customer();

// Getter
var getter = Eval.Compile>("c.Name;", "c");
            
// Setter
var setter = Eval.Compile>("c.Name = value;", "c", "value");

// Get Value
var currentValue = getter(customer);

// Set Value           
setter(customer, "ZZZ Projects");

Discussion

Using compiling expression is, of course, slower than using native API but it’s however around 2000% faster than reflection.

A lot of benchmarks with different number can be found comparing different technique to get and set value.