1414using Xtensive . Orm . Linq . Expressions ;
1515using Xtensive . Orm . Linq . Expressions . Visitors ;
1616using System . Linq ;
17+ using System . Net . Http . Headers ;
1718
1819namespace Xtensive . Orm . Linq
1920{
@@ -33,24 +34,30 @@ public Expression BindParameter(ParameterExpression parameter, Dictionary<Expres
3334 {
3435 Func < Expression , Expression > genericBinder =
3536 e => GenericExpressionVisitor < IMappedExpression > . Process ( e , mapped => mapped . BindParameter ( parameter , processedExpressions ) ) ;
37+
3638 return new ConstructorExpression (
3739 Type ,
3840 Bindings . ToDictionary ( kvp => kvp . Key , kvp => genericBinder ( kvp . Value ) , Bindings . Count ) ,
3941 NativeBindings . ToDictionary ( kvp=> kvp . Key , kvp => genericBinder ( kvp . Value ) , NativeBindings . Count ) ,
4042 Constructor ,
41- ConstructorArguments . Select ( genericBinder ) . ToArray ( ) ) ;
43+ ReferenceEquals ( ConstructorArguments , Array . Empty < Expression > ( ) )
44+ ? ConstructorArguments
45+ : ConstructorArguments . Select ( genericBinder ) . ToArray ( ) ) ;
4246 }
4347
4448 public Expression RemoveOuterParameter ( Dictionary < Expression , Expression > processedExpressions )
4549 {
4650 Func < Expression , Expression > genericRemover =
4751 e => GenericExpressionVisitor < IMappedExpression > . Process ( e , mapped => mapped . RemoveOuterParameter ( processedExpressions ) ) ;
52+
4853 var result = new ConstructorExpression (
4954 Type ,
5055 Bindings . ToDictionary ( kvp => kvp . Key , kvp => genericRemover ( kvp . Value ) ) ,
5156 NativeBindings = NativeBindings . ToDictionary ( kvp => kvp . Key , kvp => genericRemover ( kvp . Value ) ) ,
5257 Constructor ,
53- ConstructorArguments . Select ( genericRemover ) . ToArray ( ) ) ;
58+ ReferenceEquals ( ConstructorArguments , Array . Empty < Expression > ( ) )
59+ ? ConstructorArguments
60+ : ConstructorArguments . Select ( genericRemover ) . ToArray ( ) ) ;
5461 return result ;
5562 }
5663
@@ -62,8 +69,11 @@ public Expression Remap(int offset, Dictionary<Expression, Expression> processed
6269 return mapped . Remap ( offset , new Dictionary < Expression , Expression > ( ) ) ;
6370 return ( Expression ) mapped ;
6471 } ;
72+
6573 var newBindings = Bindings . ToDictionary ( kvp => kvp . Key , kvp => GenericExpressionVisitor < IMappedExpression > . Process ( kvp . Value , remapper ) ) ;
66- var newConstructorArguments = ConstructorArguments . Select ( arg => GenericExpressionVisitor < IMappedExpression > . Process ( arg , remapper ) ) ;
74+ var newConstructorArguments = ReferenceEquals ( ConstructorArguments , Array . Empty < Expression > ( ) )
75+ ? ConstructorArguments
76+ : ConstructorArguments . Select ( arg => GenericExpressionVisitor < IMappedExpression > . Process ( arg , remapper ) ) ;
6777 var newNativeBindings = NativeBindings . ToDictionary ( kvp => kvp . Key , kvp => GenericExpressionVisitor < IMappedExpression > . Process ( kvp . Value , remapper ) ) ;
6878 var result = new ConstructorExpression (
6979 Type ,
@@ -82,18 +92,27 @@ public Expression Remap(IReadOnlyList<int> map, Dictionary<Expression, Expressio
8292 return mapped . Remap ( map , new Dictionary < Expression , Expression > ( ) ) ;
8393 return ( Expression ) mapped ;
8494 } ;
95+
8596 var newBindings = Bindings . ToDictionary ( kvp => kvp . Key , kvp => GenericExpressionVisitor < IMappedExpression > . Process ( kvp . Value , remapper ) ) ;
86- var newConstructorArguments = ConstructorArguments . Select ( arg => GenericExpressionVisitor < IMappedExpression > . Process ( arg , remapper ) ) ;
97+ var newConstructorArguments = ReferenceEquals ( ConstructorArguments , Array . Empty < Expression > ( ) )
98+ ? ConstructorArguments
99+ : ConstructorArguments . Select ( arg => GenericExpressionVisitor < IMappedExpression > . Process ( arg , remapper ) ) ;
87100 var newNativeBindings = NativeBindings . ToDictionary ( kvp => kvp . Key , kvp => GenericExpressionVisitor < IMappedExpression > . Process ( kvp . Value , remapper ) ) ;
88101 return new ConstructorExpression ( Type , newBindings , newNativeBindings , Constructor , newConstructorArguments ) ;
89102 }
90103
91- public ConstructorExpression ( Type type , Dictionary < MemberInfo , Expression > bindings , Dictionary < MemberInfo , Expression > nativeBindings , ConstructorInfo constructor , IEnumerable < Expression > constructorArguments )
104+ public ConstructorExpression ( Type type ,
105+ Dictionary < MemberInfo , Expression > bindings ,
106+ Dictionary < MemberInfo , Expression > nativeBindings ,
107+ ConstructorInfo constructor ,
108+ IEnumerable < Expression > constructorArguments )
92109 : base ( ExtendedExpressionType . Constructor , type , null , false )
93110 {
94111 Bindings = bindings ?? new Dictionary < MemberInfo , Expression > ( ) ;
95112 NativeBindings = nativeBindings ;
96- ConstructorArguments = constructorArguments ?? Enumerable . Empty < Expression > ( ) ;
113+ // consistently use keep empty array instead of any other empty collection,
114+ // there are checks that help to not instanciate collections
115+ ConstructorArguments = constructorArguments ?? Array . Empty < Expression > ( ) ;
97116 Constructor = constructor ;
98117 }
99118 }
0 commit comments