diff --git a/Orm/Xtensive.Orm/Linq/ExpressionExtensions.cs b/Orm/Xtensive.Orm/Linq/ExpressionExtensions.cs index 24a7eb5e08..caddeaec1e 100644 --- a/Orm/Xtensive.Orm/Linq/ExpressionExtensions.cs +++ b/Orm/Xtensive.Orm/Linq/ExpressionExtensions.cs @@ -5,6 +5,7 @@ // Created: 2009.04.21 using System; +using System.Collections.Concurrent; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -20,7 +21,10 @@ namespace Xtensive.Linq /// public static class ExpressionExtensions { - private static readonly MethodInfo TupleGenericAccessor; + private static readonly ConcurrentDictionary valueAccessors = + new ConcurrentDictionary(); + + private static readonly Func TupleValueAccessorFactory; /// /// Makes method call. @@ -29,15 +33,12 @@ public static class ExpressionExtensions ///Type of accessor. ///Tuple field index. /// - public static MethodCallExpression MakeTupleAccess(this Expression target, Type accessorType, int index) - { - return Expression.Call( + public static MethodCallExpression MakeTupleAccess(this Expression target, Type accessorType, int index) => + Expression.Call( target, - TupleGenericAccessor.MakeGenericMethod(accessorType), + valueAccessors.GetOrAdd(accessorType, TupleValueAccessorFactory), Expression.Constant(index) - ); - } - + ); /// /// Makes IsNull condition expression. @@ -46,41 +47,38 @@ public static MethodCallExpression MakeTupleAccess(this Expression target, Type /// Result expression if is . /// Result expression if is not . /// - public static ConditionalExpression MakeIsNullCondition(this Expression target, Expression ifNull, Expression ifNotNull) - { - return Expression.Condition( + public static ConditionalExpression MakeIsNullCondition( + this Expression target, Expression ifNull, Expression ifNotNull) => + Expression.Condition( Expression.Equal(target, Expression.Constant(null, target.Type)), - ifNull, ifNotNull - ); - } + ifNull, + ifNotNull + ); /// /// Converts expression type to nullable type (for value types). /// /// The expression. - public static Expression LiftToNullable(this Expression expression) - { - return expression.Type.IsNullable() - ? expression - : Expression.Convert(expression, expression.Type.ToNullable()); - } + public static Expression LiftToNullable(this Expression expression) => + expression.Type.IsNullable() + ? expression + : Expression.Convert(expression, expression.Type.ToNullable()); /// /// Converts specified to . /// /// The expression to convert. /// Expression tree that wraps . - public static ExpressionTree ToExpressionTree(this Expression expression) - { - return new ExpressionTree(expression); - } + public static ExpressionTree ToExpressionTree(this Expression expression) => new ExpressionTree(expression); // Type initializer static ExpressionExtensions() { - TupleGenericAccessor = typeof (Tuple).GetMethods().Single(mi => mi.Name==WellKnown.Tuple.GetValueOrDefault && mi.IsGenericMethod); + var tupleGenericAccessor = typeof(Tuple).GetMethods() + .Single(mi => mi.Name == WellKnown.Tuple.GetValueOrDefault && mi.IsGenericMethod); + TupleValueAccessorFactory = type => tupleGenericAccessor.MakeGenericMethod(type); } } } \ No newline at end of file