CodeProject Many times I need to get or set properties on a class dynamically, i.e. not knowing their exact names at the compile times. Usually I was doing it using System.Reflection API’s PropertyInfo class. This class provides GetValue() and SetValue methods that allow extrating or setting a value of a C# property based on the the propertie’s name. The problem with this approach is that accessing a property dynamically using reflection API is very slow in comparison to accessing it via usual static API – in my tests the difference was more than 60-fold.
System.Reflection
PropertyInfo
GetValue()
SetValue
I thought about using LINQ’s expression trees, instead. Nice thing about the expression trees is that they can be compiled in a way very similar to static compilation. It turned out that there are already examples of Expression getters and setters available on the internet e.g. from Using expression trees to get property getter and setters and Creating a property setter delegate.
Based on the code described at the above URL, I built my own little Expression based getter and setter library:
Expression
// returns property getter public static Func<TObject, TProperty> GetPropGetter<TObject, TProperty>(string propertyName) { ParameterExpression paramExpression = Expression.Parameter(typeof(TObject), "value"); Expression propertyGetterExpression = Expression.Property(paramExpression, propertyName); Func<TObject, TProperty> result = Expression.Lambda<Func<TObject, TProperty>>(propertyGetterExpression, paramExpression).Compile(); return result; } // returns property setter: public static Action<TObject, TProperty> GetPropSetter<TObject, TProperty>(string propertyName) { ParameterExpression paramExpression = Expression.Parameter(typeof(TObject)); ParameterExpression paramExpression2 = Expression.Parameter(typeof(TProperty), propertyName); MemberExpression propertyGetterExpression = Expression.Property(paramExpression, propertyName); Action<TObject, TProperty> result = Expression.Lambda<Action<TObject, TProperty>> ( Expression.Assign(propertyGetterExpression, paramExpression2), paramExpression, paramExpression2 ).Compile(); return result; }
I also did some benchmarking comparing the speed of these getters and setters to those of
Here are the results of running the getters and setters on 100000000 different objects with string properies:
Getters
Setters
You can see, that even though the dynamically compiled expressions are 3-5 times slower than statically compiled direct methods and 2-3 times slower than statically compiled lambdas, they are 18-20 times faster than Reflection based approach.
The code for the demo is located under CompiledExpressionTests.zip