From bd9fc5dc687593d831bc29f90f7ed0b53f1c8736 Mon Sep 17 00:00:00 2001 From: Muchiachio Date: Mon, 26 Oct 2015 19:19:18 +0200 Subject: [PATCH] Using IsDefined over GetCustomAttribute - Replaced Type.GetCustomAttribute with Type.IsDefined where the attribute instance is only used to check if it's defined, to increase performance. Resolves #3416 --- .../DataAnnotationsMetadataProvider.cs | 3 +-- .../ModelBinding/DataMemberRequiredBindingMetadataProvider.cs | 2 +- .../ViewComponents/ViewComponentConventions.cs | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsMetadataProvider.cs b/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsMetadataProvider.cs index 39a992c8ae..426f515a52 100644 --- a/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsMetadataProvider.cs +++ b/src/Microsoft.AspNet.Mvc.DataAnnotations/DataAnnotationsMetadataProvider.cs @@ -112,8 +112,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata displayMetadata.IsEnum = true; // IsFlagsEnum - displayMetadata.IsFlagsEnum = - underlyingTypeInfo.GetCustomAttribute(inherit: false) != null; + displayMetadata.IsFlagsEnum = underlyingTypeInfo.IsDefined(typeof(FlagsAttribute), inherit: false); // EnumDisplayNamesAndValues and EnumNamesAndValues // diff --git a/src/Microsoft.AspNet.Mvc.Formatters.Xml/ModelBinding/DataMemberRequiredBindingMetadataProvider.cs b/src/Microsoft.AspNet.Mvc.Formatters.Xml/ModelBinding/DataMemberRequiredBindingMetadataProvider.cs index 80235a3eb1..4cddf6d4ca 100644 --- a/src/Microsoft.AspNet.Mvc.Formatters.Xml/ModelBinding/DataMemberRequiredBindingMetadataProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Formatters.Xml/ModelBinding/DataMemberRequiredBindingMetadataProvider.cs @@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata // isDataContract == true iff the container type has at least one DataContractAttribute var containerType = context.Key.ContainerType.GetTypeInfo(); - var isDataContract = containerType.GetCustomAttribute() != null; + var isDataContract = containerType.IsDefined(typeof(DataContractAttribute)); if (isDataContract) { // We don't need to add a validator, just to set IsRequired = true. The validation diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs index a0e459fd41..8227c39d39 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs @@ -89,7 +89,7 @@ namespace Microsoft.AspNet.Mvc.ViewComponents return typeInfo.Name.EndsWith(ViewComponentSuffix, StringComparison.OrdinalIgnoreCase) || - typeInfo.GetCustomAttribute() != null; + typeInfo.IsDefined(typeof(ViewComponentAttribute)); } } }