Using Try Catch is slow.

Apr 19, 2015 at 9:54 PM
Edited Apr 19, 2015 at 9:54 PM

Awesome Code, Thanks for sharing.

I replaced some of you try catches with methods. Try catch should always be the last resort. it is very slow. I'd like to replace all the try catches in your evaluate methods too. But I think I need to understand them better to do that. Here is the code i used if you wanted to update yours.
                var lambda = Expression.Lambda<Func<decimal>>(result);
                return lambda;

        Expression expressionForPropsOrConstant;

        if (CanCovertTo(expression, typeof (decimal)))
            expressionForPropsOrConstant = Expression.Constant(Convert.ChangeType(expression, typeof (decimal)));
            expressionForPropsOrConstant = MemberExpression.Property(paramExpression, expression);

        return expressionForPropsOrConstant;

    /// <summary>
    /// Check if Convert will work on the type & value
    /// </summary>
    /// <param name="testString"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    private static bool CanCovertTo(string testString, Type type)
        TypeConverter converter = TypeDescriptor.GetConverter(type);
        return converter.IsValid(testString);