protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
Func<IEnumerable<Attribute>, ModelMetadata> metadataFactory = (attr) => base.CreateMetadata(attr, containerType, modelAccessor, modelType, propertyName);
var conventionType = containerType ?? modelType;
var defaultResourceType = DefaultResourceType;
var conventionAttribute = conventionType.GetAttributeOnTypeOrAssembly<MetadataConventionsAttribute>();
if (conventionAttribute != null && conventionAttribute.ResourceType != null)
{
defaultResourceType = conventionAttribute.ResourceType;
}
else if (RequireConventionAttribute)
{
return metadataFactory(attributes);
}
ApplyConventionsToValidationAttributes(attributes, containerType, propertyName, defaultResourceType);
var foundDisplayAttribute = attributes.FirstOrDefault(a => a is DisplayAttribute) as DisplayAttribute;
if (foundDisplayAttribute.CanSupplyDisplayName())
{
return metadataFactory(attributes);
}
// Our displayAttribute is lacking. Time to get busy.
DisplayAttribute displayAttribute = foundDisplayAttribute.Copy() ?? new DisplayAttribute();
var rewrittenAttributes = attributes.Replace(foundDisplayAttribute, displayAttribute);
// ensure resource type.
displayAttribute.ResourceType = displayAttribute.ResourceType ?? defaultResourceType;
if (displayAttribute.ResourceType != null)
{
// ensure resource name
string displayAttributeName = GetDisplayAttributeName(containerType, propertyName, displayAttribute);
if (displayAttributeName != null)
{
displayAttribute.Name = displayAttributeName;
}
if (!displayAttribute.ResourceType.PropertyExists(displayAttribute.Name))
{
displayAttribute.ResourceType = null;
}
}
var metadata = metadataFactory(rewrittenAttributes);
if (metadata.DisplayName == metadata.PropertyName)
{
metadata.DisplayName = metadata.DisplayName.SplitUpperCaseToString();
}
return metadata;
}