Skip to content

Commit 98673d4

Browse files
committed
Address review feedback
1 parent a83a24e commit 98673d4

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/Mvc/Mvc.Core/src/ModelBinding/Metadata/ModelAttributes.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,12 @@ public static ModelAttributes GetAttributesForParameter(ParameterInfo parameterI
225225
// To avoid this, we call `GetCustomAttributes` directly
226226
// to avoid examining the inheritance hierarchy.
227227
// See https://source.dot.net/#System.Private.CoreLib/src/System/Attribute.CoreCLR.cs,677
228-
var modelMetadataTypeAttributes = type.GetCustomAttributes<ModelMetadataTypeAttribute>(inherit: false);
229-
try
228+
var modelMetadataTypeAttributes = type.GetCustomAttributes<ModelMetadataTypeAttribute>(inherit: false).ToArray();
229+
return modelMetadataTypeAttributes switch
230230
{
231-
return modelMetadataTypeAttributes?.SingleOrDefault()?.MetadataType;
232-
}
233-
catch (InvalidOperationException e)
234-
{
235-
throw new InvalidOperationException("Only one ModelMetadataType attribute is permitted per type.", e);
236-
}
231+
[var attribute] => attribute.MetadataType,
232+
[] => null,
233+
[..] => throw new InvalidOperationException($"Multiple ModelMetadataType attributes on {type.FullName}. Only one ModelMetadataType attribute is permitted per type."),
234+
};
237235
}
238236
}

src/Mvc/Mvc.Core/test/ModelBinding/Metadata/ModelAttributesTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void GetAttributeForProperty_WithModelType_HandlesMultipleAttributesOnTyp
296296

297297
// Assert
298298
var exception = Assert.Throws<InvalidOperationException>(() => ModelAttributes.GetAttributesForProperty(modelType, property));
299-
Assert.Equal("Only one ModelMetadataType attribute is permitted per type.", exception.Message);
299+
Assert.Equal("Multiple ModelMetadataType attributes on Microsoft.AspNetCore.Mvc.ModelBinding.ModelAttributesTest+InvalidBaseViewModel. Only one ModelMetadataType attribute is permitted per type.", exception.Message);
300300
}
301301

302302
[ClassValidator]

0 commit comments

Comments
 (0)