@@ -120,12 +120,17 @@ private void OnValidationRequested(object? sender, ValidationRequestedEventArgs
120
120
{
121
121
var validationContext = new ValidationContext ( _editContext . Model , _serviceProvider , items : null ) ;
122
122
123
- if ( TryValidateTypeInfo ( validationContext ) )
123
+ if ( ! TryValidateTypeInfo ( validationContext ) )
124
124
{
125
- _editContext . NotifyValidationStateChanged ( ) ;
126
- return ;
125
+ ValidateWithDefaultValidator ( validationContext ) ;
127
126
}
128
127
128
+ _editContext . NotifyValidationStateChanged ( ) ;
129
+ }
130
+
131
+ [ UnconditionalSuppressMessage ( "Trimming" , "IL2026" , Justification = "Model types are expected to be defined in assemblies that do not get trimmed." ) ]
132
+ private void ValidateWithDefaultValidator ( ValidationContext validationContext )
133
+ {
129
134
var validationResults = new List < ValidationResult > ( ) ;
130
135
Validator . TryValidateObject ( _editContext . Model , validationContext , validationResults , true ) ;
131
136
@@ -150,8 +155,6 @@ private void OnValidationRequested(object? sender, ValidationRequestedEventArgs
150
155
_messages . Add ( new FieldIdentifier ( _editContext . Model , fieldName : string . Empty ) , validationResult . ErrorMessage ! ) ;
151
156
}
152
157
}
153
-
154
- _editContext . NotifyValidationStateChanged ( ) ;
155
158
}
156
159
157
160
#pragma warning disable ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
@@ -199,9 +202,10 @@ private bool TryValidateTypeInfo(ValidationContext validationContext)
199
202
#pragma warning restore ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
200
203
201
204
// TODO(OR): Replace this with a more robust implementation or a different approach. Ideally, collect references during the validation process itself.
205
+ [ UnconditionalSuppressMessage ( "Trimming" , "IL2075" , Justification = "Model types are expected to be defined in assemblies that do not get trimmed." ) ]
202
206
private static object GetFieldContainer ( object obj , string [ ] dotSegments )
203
207
{
204
- // The method does not check nullity and index bounds everywhere as the path is constructed internally and assumed to be correct.
208
+ // The method does not check all possiblle null access and index bound errors as the path is constructed internally and assumed to be correct.
205
209
object currentObject = obj ;
206
210
207
211
for ( int i = 0 ; i < dotSegments . Length ; i ++ )
@@ -224,10 +228,14 @@ private static object GetFieldContainer(object obj, string[] dotSegments)
224
228
string ? indexStr = match . Groups [ 2 ] . Success ? match . Groups [ 2 ] . Value : null ;
225
229
226
230
Type currentType = currentObject . GetType ( ) ;
227
- PropertyInfo propertyInfo = currentType . GetProperty ( propertyName , BindingFlags . Public | BindingFlags . Instance | BindingFlags . IgnoreCase ) ;
231
+ PropertyInfo propertyInfo = currentType ! . GetProperty ( propertyName , BindingFlags . Public | BindingFlags . Instance ) ! ;
228
232
object propertyValue = propertyInfo ! . GetValue ( currentObject ) ! ;
229
233
230
- if ( indexStr != null ) // Indexed access
234
+ if ( indexStr == null ) // Simple property access
235
+ {
236
+ currentObject = propertyValue ;
237
+ }
238
+ else // Indexed access
231
239
{
232
240
if ( ! int . TryParse ( indexStr , out int index ) )
233
241
{
@@ -251,10 +259,7 @@ private static object GetFieldContainer(object obj, string[] dotSegments)
251
259
throw new ArgumentException ( $ "Property '{ propertyName } ' is not an array, list, or enumerable. Cannot access by index in segment '{ segment } '.") ;
252
260
}
253
261
}
254
- else // Simple property access
255
- {
256
- currentObject = propertyValue ;
257
- }
262
+
258
263
}
259
264
return currentObject ! ;
260
265
}
0 commit comments