Reflection Performance – Create Delegate (Properties C#)

This should work for you: static Action<object, object> BuildSetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), “o”); var value = Expression.Parameter(typeof(object)); Expression<Action<object, object>> expr = Expression.Lambda<Action<object, object>>( Expression.Call( Expression.Convert(obj, method.DeclaringType), method, Expression.Convert(value, method.GetParameters()[0].ParameterType)), obj, value); return expr.Compile(); } Usage: var accessor = BuildSetAccessor(typeof(TestClass).GetProperty(“MyProperty”).GetSetMethod()); var instance = new TestClass(); accessor(instance, “foo”); Console.WriteLine(instance.MyProperty); With TestClass: public class TestClass … Read more

Overriding grails.views.default.codec=’html’ config back to ‘none’

To summarize the various levels at which the codec can be applied: Set Config.groovy’s grails.views.default.codec=”html” to get HTML escaping by default on all ${expressions} in the application. Then when you want to default a whole page back to none, use the directive: <%@page defaultCodec=”none” %> or <%@ defaultCodec=”none” %> To disable HTML encoding for one … Read more

C fundamentals: double variable not equal to double expression?

I suspect you’re using 32-bit x86, the only common architecture subject to excess precision. In C, expressions of type float and double are actually evaluated as float_t or double_t, whose relationships to float and double are reflected in the FLT_EVAL_METHOD macro. In the case of x86, both are defined as long double because the fpu … Read more

Expression for Type members results in different Expressions (MemberExpression, UnaryExpression)

The reason this happens is that Age is a value type. In order to coerce an expression returning a value type into Func<Person,object> the compiler needs to insert a Convert(expr, typeof(object)), a UnaryExpression. For strings and other reference types, however, there is no need to box, so a “straight” member expression is returned. If you … Read more

How to set property value using Expressions? [duplicate]

You could cheat and make life easier with an extension method: public static class LambdaExtensions { public static void SetPropertyValue<T, TValue>(this T target, Expression<Func<T, TValue>> memberLamda, TValue value) { var memberSelectorExpression = memberLamda.Body as MemberExpression; if (memberSelectorExpression != null) { var property = memberSelectorExpression.Member as PropertyInfo; if (property != null) { property.SetValue(target, value, null); } … Read more