@@ -774,20 +774,6 @@ private static Expression BuildNestedEnumerableSelector(Expression source, Param
774774 return call ;
775775 }
776776
777- private static IEnumerable < NestedEnumerablePair < TParent , TChild > > FlattenNestedEnumerable < TParent , TChild > ( IEnumerable < TParent > source , Func < TParent , IEnumerable < TChild > > collectionSelector )
778- {
779- int parentIndex = 1 ;
780- foreach ( TParent parent in source )
781- {
782- int childIndex = 1 ;
783- foreach ( TChild child in collectionSelector ( parent ) )
784- {
785- yield return new NestedEnumerablePair < TParent , TChild > ( parentIndex , childIndex ++ , parent , child ) ;
786- }
787- parentIndex ++ ;
788- }
789- }
790-
791777 private static MethodInfo GetStructuredTypeAddMethod ( Type type )
792778 {
793779 MethodInfo addMethod = type . SafeGetMethod ( "Add" , BindingFlags . Public | BindingFlags . Instance ) ;
@@ -832,6 +818,80 @@ private static void CollectApiParameters(HttpParameterResolutionMethod method, I
832818 method . AddParameter ( parameter . ApiParameterName , parameter . ParameterType , parameter . Location , parameter . IsOptional ) ;
833819 }
834820
821+ private static bool IsUri ( HttpParameterLocation location ) => location is HttpParameterLocation . Query or HttpParameterLocation . Path ;
822+
823+ private static Exception CreateException ( string message , Exception innerException , IHttpActionDescriptor action , string parameterName )
824+ {
825+ StringBuilder sb = new StringBuilder ( message ) ;
826+ if ( action != null )
827+ {
828+ sb . AppendLine ( )
829+ . Append ( "at " )
830+ . Append ( action . Method . ToString ( ) . ToUpperInvariant ( ) )
831+ . Append ( ' ' )
832+ . Append ( action . Uri ) ;
833+ }
834+
835+ if ( parameterName != null )
836+ {
837+ sb . AppendLine ( )
838+ . Append ( "Parameter: " )
839+ . Append ( parameterName ) ;
840+
841+ if ( action != null && action . TryGetParameter ( parameterName , out HttpParameterSource source ) )
842+ {
843+ sb . AppendLine ( )
844+ . Append ( "Source: " )
845+ . Append ( source . Description ) ;
846+ }
847+ }
848+
849+ return new InvalidOperationException ( sb . ToString ( ) , innerException ) ;
850+ }
851+
852+ private static MethodInfo GetStructuredTypeFactoryMethod ( Type implementationType , Type itemType , bool withIndex )
853+ {
854+ foreach ( MethodInfo method in typeof ( StructuredType < > ) . MakeGenericType ( implementationType ) . GetRuntimeMethods ( ) )
855+ {
856+ if ( method . Name != "From" )
857+ continue ;
858+
859+ IList < ParameterInfo > parameters = method . GetParameters ( ) ;
860+ if ( parameters . Count != 2 )
861+ continue ;
862+
863+ if ( parameters [ 0 ] . ParameterType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > )
864+ && parameters [ 1 ] . ParameterType . GetGenericTypeDefinition ( ) == ( withIndex ? typeof ( Action < , , > ) : typeof ( Action < , > ) ) )
865+ {
866+ return method . MakeGenericMethod ( itemType ) ;
867+ }
868+ }
869+ throw new InvalidOperationException ( "Could not find structured type factory method 'StructuredType<>.From()'" ) ;
870+ }
871+
872+ private static Type TryGetEnumerableType ( Type type )
873+ {
874+ if ( ! type . IsGenericType )
875+ return null ;
876+
877+ if ( type . GetInterfaces ( ) . All ( x => x . GetGenericTypeDefinition ( ) != typeof ( IEnumerable < > ) ) )
878+ return null ;
879+
880+ return type . GenericTypeArguments [ 0 ] ;
881+
882+ }
883+
884+ private static Type GetEnumerableType ( Type type )
885+ {
886+ Type itemType = TryGetEnumerableType ( type ) ;
887+ if ( itemType == null )
888+ throw new InvalidOperationException ( $ "Type does not implement IEnumerable<>: { type } ") ;
889+
890+ return itemType ;
891+ }
892+ #endregion
893+
894+ #region Runtime methods used by generated LINQ expression tree
835895 private static void AddParameterFromBody < TSource , TTarget , TConverter > ( IDictionary < string , object > arguments , string parameterName ) where TConverter : IFormattedInputConverter < TSource , TTarget > , new ( )
836896 {
837897 TTarget target = ConvertParameterFromBody < TSource , TTarget , TConverter > ( arguments ) ;
@@ -853,21 +913,6 @@ private static void CollectApiParameters(HttpParameterResolutionMethod method, I
853913 return target ;
854914 }
855915
856- private static IEnumerable < string > GetClaimValues ( ClaimsPrincipal principal , string claimType )
857- {
858- IEnumerable < string > values = principal . Claims
859- . Where ( x => x . Type == claimType )
860- . Select ( x => x . Value ) ;
861- return values ;
862- }
863-
864- private static string GetClaimValue ( ClaimsPrincipal principal , string claimType )
865- {
866- IEnumerable < string > values = GetClaimValues ( principal , claimType ) ;
867- string value = values . FirstOrDefault ( ) ;
868- return value ;
869- }
870-
871916 private static TTarget ConvertValue < TSource , TTarget > ( string parameterName , TSource value , IHttpActionDescriptor action )
872917 {
873918 try
@@ -909,76 +954,33 @@ private static TTarget ConvertValue<TSource, TTarget>(string parameterName, TSou
909954 }
910955 }
911956
912- private static bool IsUri ( HttpParameterLocation location ) => location is HttpParameterLocation . Query or HttpParameterLocation . Path ;
913-
914- private static Exception CreateException ( string message , Exception innerException , IHttpActionDescriptor action , string parameterName )
915- {
916- StringBuilder sb = new StringBuilder ( message ) ;
917- if ( action != null )
918- {
919- sb . AppendLine ( )
920- . Append ( "at " )
921- . Append ( action . Method . ToString ( ) . ToUpperInvariant ( ) )
922- . Append ( ' ' )
923- . Append ( action . Uri ) ;
924- }
925-
926- if ( parameterName != null )
927- {
928- sb . AppendLine ( )
929- . Append ( "Parameter: " )
930- . Append ( parameterName ) ;
931-
932- if ( action != null && action . TryGetParameter ( parameterName , out HttpParameterSource source ) )
933- {
934- sb . AppendLine ( )
935- . Append ( "Source: " )
936- . Append ( source . Description ) ;
937- }
938- }
939-
940- return new InvalidOperationException ( sb . ToString ( ) , innerException ) ;
941- }
942-
943- private static MethodInfo GetStructuredTypeFactoryMethod ( Type implementationType , Type itemType , bool withIndex )
957+ private static IEnumerable < NestedEnumerablePair < TParent , TChild > > FlattenNestedEnumerable < TParent , TChild > ( IEnumerable < TParent > source , Func < TParent , IEnumerable < TChild > > collectionSelector )
944958 {
945- foreach ( MethodInfo method in typeof ( StructuredType < > ) . MakeGenericType ( implementationType ) . GetRuntimeMethods ( ) )
959+ int parentIndex = 1 ;
960+ foreach ( TParent parent in source )
946961 {
947- if ( method . Name != "From" )
948- continue ;
949-
950- IList < ParameterInfo > parameters = method . GetParameters ( ) ;
951- if ( parameters . Count != 2 )
952- continue ;
953-
954- if ( parameters [ 0 ] . ParameterType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > )
955- && parameters [ 1 ] . ParameterType . GetGenericTypeDefinition ( ) == ( withIndex ? typeof ( Action < , , > ) : typeof ( Action < , > ) ) )
962+ int childIndex = 1 ;
963+ foreach ( TChild child in collectionSelector ( parent ) )
956964 {
957- return method . MakeGenericMethod ( itemType ) ;
965+ yield return new NestedEnumerablePair < TParent , TChild > ( parentIndex , childIndex ++ , parent , child ) ;
958966 }
967+ parentIndex ++ ;
959968 }
960- throw new InvalidOperationException ( "Could not find structured type factory method 'StructuredType<>.From()'" ) ;
961969 }
962970
963- private static Type TryGetEnumerableType ( Type type )
971+ private static string GetClaimValue ( ClaimsPrincipal principal , string claimType )
964972 {
965- if ( ! type . IsGenericType )
966- return null ;
967-
968- if ( type . GetInterfaces ( ) . All ( x => x . GetGenericTypeDefinition ( ) != typeof ( IEnumerable < > ) ) )
969- return null ;
970-
971- return type . GenericTypeArguments [ 0 ] ;
972-
973+ IEnumerable < string > values = GetClaimValues ( principal , claimType ) ;
974+ string value = values . FirstOrDefault ( ) ;
975+ return value ;
973976 }
974977
975- private static Type GetEnumerableType ( Type type )
978+ private static IEnumerable < string > GetClaimValues ( ClaimsPrincipal principal , string claimType )
976979 {
977- Type itemType = TryGetEnumerableType ( type ) ;
978- if ( itemType == null )
979- throw new InvalidOperationException ( $ "Type does not implement IEnumerable<>: { type } ") ;
980-
981- return itemType ;
980+ IEnumerable < string > values = principal . Claims
981+ . Where ( x => x . Type == claimType )
982+ . Select ( x => x . Value ) ;
983+ return values ;
982984 }
983985 #endregion
984986
0 commit comments