Update to ExpressionExtensions

Feb 3, 2016 at 9:07 PM
Not sure where best to put this, so I'll put it here. I've updated ExpressionExpander.Evaluate() to allow for statically defined expressions:
        internal static object Evaluate(Expression e)
        {
            if (e is ConstantExpression) return ((ConstantExpression)e).Value;
            if (e is MemberExpression)
            {
                var me = (MemberExpression)e;
                if (me.Expression == null) // the member is static
                    return me.Member
                        .DeclaringType
                        .GetField(me.Member.Name, BindingFlags.Static | BindingFlags.Public)
                        .GetValue(null);
                var obj = Evaluate(me.Expression);
                var prop = (me.Member as PropertyInfo);
                if (prop != null) return prop.GetValue(obj, new object[0]);
                var fld = (me.Member as FieldInfo);
                if (fld != null) return fld.GetValue(obj);
            }
            if (e is NewArrayExpression)
            {
                var ar = (NewArrayExpression)e;
                var args = ar.Expressions.Select((expr) => Evaluate(expr)).ToArray();
                var res = Array.CreateInstance(ar.Type.GetElementType(), args.Length);
                for (int i = 0; i < res.Length; i++)
                    res.SetValue(args[i], i);
                return res;
            }
            throw new Exception("Cannot evaluate expression!");
        }
Thanks for a brilliant library! This allows us to dramatically improve performance in a maintainable manner.