@@ -32,21 +32,13 @@ public override bool CanConvert(Type typeToConvert)
3232 {
3333 var constructor = constructors [ 0 ] ;
3434 var parameters = constructor . GetParameters ( ) ;
35- var hasParameters = parameters . Length > 0 ;
36- if ( hasParameters )
35+
36+ if ( parameters . Length > 0 )
3737 {
3838 var properties = typeToConvert . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
39- result = true ;
40- foreach ( var parameter in parameters )
41- {
42- var hasMatchingProperty = properties . Any ( p =>
43- NameOfPropertyAndParameter . Matches ( p . Name , parameter . Name , typeToConvert . IsAnonymous ( ) ) ) ;
44- if ( ! hasMatchingProperty )
45- {
46- result = false ;
47- break ;
48- }
49- }
39+ result = parameters
40+ . Select ( parameter => properties . Any ( p => NameOfPropertyAndParameter . Matches ( p . Name , parameter . Name , typeToConvert . IsAnonymous ( ) ) ) )
41+ . All ( hasMatchingProperty => hasMatchingProperty ) ;
5042 }
5143 else
5244 {
@@ -69,8 +61,8 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
6961 break ;
7062 }
7163
72- var jsonPropName = reader . GetString ( ) ;
73- var normalizedPropName = ConvertAndNormalizeName ( jsonPropName , options ) ;
64+ string jsonPropName = reader . GetString ( ) ;
65+ string normalizedPropName = ConvertAndNormalizeName ( jsonPropName , options ) ;
7466 if ( ! namedPropertiesMapping . TryGetValue ( normalizedPropName , out var obProp ) )
7567 {
7668 reader . Read ( ) ;
@@ -86,7 +78,7 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
8678 var ctor = typeToConvert . GetConstructors ( BindingFlags . Public | BindingFlags . Instance ) . First ( ) ;
8779 var parameters = ctor . GetParameters ( ) ;
8880 var parameterValues = new object [ parameters . Length ] ;
89- for ( var index = 0 ; index < parameters . Length ; index ++ )
81+ for ( int index = 0 ; index < parameters . Length ; index ++ )
9082 {
9183 var parameterInfo = parameters [ index ] ;
9284 var value = valueOfProperty . First ( prop =>
@@ -142,7 +134,7 @@ private static IReadOnlyDictionary<string, PropertyInfo> GetNamedProperties(Json
142134 name = options . PropertyNamingPolicy ? . ConvertName ( property . Name ) ?? property . Name ;
143135 }
144136
145- var normalizedName = NormalizeName ( name , options ) ;
137+ string normalizedName = NormalizeName ( name , options ) ;
146138 result . Add ( normalizedName , property ) ;
147139 }
148140
@@ -151,8 +143,8 @@ private static IReadOnlyDictionary<string, PropertyInfo> GetNamedProperties(Json
151143
152144 private static string ConvertAndNormalizeName ( string name , JsonSerializerOptions options )
153145 {
154- var convertedName = options . PropertyNamingPolicy ? . ConvertName ( name ) ?? name ;
155- return options . PropertyNameCaseInsensitive ? convertedName . ToLowerInvariant ( ) : convertedName ;
146+ string convertedName = options . PropertyNamingPolicy ? . ConvertName ( name ) ?? name ;
147+ return NormalizeName ( convertedName , options ) ;
156148 }
157149
158150 private static string NormalizeName ( string name , JsonSerializerOptions options ) => options . PropertyNameCaseInsensitive ? name . ToLowerInvariant ( ) : name ;
@@ -162,12 +154,12 @@ internal static class NameOfPropertyAndParameter
162154 {
163155 public static bool Matches ( string propertyName , string parameterName , bool anonymousType )
164156 {
165- if ( propertyName is null && parameterName is null )
157+ if ( string . IsNullOrEmpty ( propertyName ) )
166158 {
167- return true ;
159+ return string . IsNullOrEmpty ( parameterName ) ;
168160 }
169161
170- if ( propertyName is null || parameterName is null )
162+ if ( string . IsNullOrEmpty ( parameterName ) )
171163 {
172164 return false ;
173165 }
@@ -176,12 +168,10 @@ public static bool Matches(string propertyName, string parameterName, bool anony
176168 {
177169 return propertyName . Equals ( parameterName , StringComparison . Ordinal ) ;
178170 }
179- else
180- {
181- var xRight = propertyName . AsSpan ( 1 ) ;
182- var yRight = parameterName . AsSpan ( 1 ) ;
183- return char . ToLowerInvariant ( propertyName [ 0 ] ) . CompareTo ( parameterName [ 0 ] ) == 0 && xRight . Equals ( yRight , StringComparison . Ordinal ) ;
184- }
171+
172+ var xRight = propertyName . AsSpan ( 1 ) ;
173+ var yRight = parameterName . AsSpan ( 1 ) ;
174+ return char . ToLowerInvariant ( propertyName [ 0 ] ) . CompareTo ( parameterName [ 0 ] ) == 0 && xRight . Equals ( yRight , StringComparison . Ordinal ) ;
185175 }
186176 }
187177
0 commit comments