Using IsDefined over GetCustomAttribute<T>

- Replaced Type.GetCustomAttribute<T> with Type.IsDefined where the
attribute instance is only used to check if it's defined, to increase
performance.

Resolves #3416
This commit is contained in:
Muchiachio 2015-10-26 19:19:18 +02:00 committed by Pranav K
parent 08be63c3fc
commit bd9fc5dc68
3 changed files with 3 additions and 4 deletions

View File

@ -112,8 +112,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
displayMetadata.IsEnum = true; displayMetadata.IsEnum = true;
// IsFlagsEnum // IsFlagsEnum
displayMetadata.IsFlagsEnum = displayMetadata.IsFlagsEnum = underlyingTypeInfo.IsDefined(typeof(FlagsAttribute), inherit: false);
underlyingTypeInfo.GetCustomAttribute<FlagsAttribute>(inherit: false) != null;
// EnumDisplayNamesAndValues and EnumNamesAndValues // EnumDisplayNamesAndValues and EnumNamesAndValues
// //

View File

@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
// isDataContract == true iff the container type has at least one DataContractAttribute // isDataContract == true iff the container type has at least one DataContractAttribute
var containerType = context.Key.ContainerType.GetTypeInfo(); var containerType = context.Key.ContainerType.GetTypeInfo();
var isDataContract = containerType.GetCustomAttribute<DataContractAttribute>() != null; var isDataContract = containerType.IsDefined(typeof(DataContractAttribute));
if (isDataContract) if (isDataContract)
{ {
// We don't need to add a validator, just to set IsRequired = true. The validation // We don't need to add a validator, just to set IsRequired = true. The validation

View File

@ -89,7 +89,7 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
return return
typeInfo.Name.EndsWith(ViewComponentSuffix, StringComparison.OrdinalIgnoreCase) || typeInfo.Name.EndsWith(ViewComponentSuffix, StringComparison.OrdinalIgnoreCase) ||
typeInfo.GetCustomAttribute<ViewComponentAttribute>() != null; typeInfo.IsDefined(typeof(ViewComponentAttribute));
} }
} }
} }