Created
November 15, 2012 09:22
-
-
Save bzbetty/4077599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<Attribute> GetMappedAttributes(this IConfigurationProvider mapper, Type sourceType, string propertyName, IEnumerable<Attribute> existingAttributes) | |
{ | |
if (sourceType != null) | |
{ | |
foreach (var typeMap in mapper.GetAllTypeMaps().Where(i => i.SourceType == sourceType)) | |
{ | |
foreach (var propertyMap in typeMap.GetPropertyMaps()) | |
{ | |
if (propertyMap.IsIgnored() || propertyMap.SourceMember == null) | |
continue; | |
if (propertyMap.SourceMember.Name == propertyName) | |
{ | |
foreach (ValidationAttribute attribute in propertyMap.DestinationProperty.GetCustomAttributes(typeof(ValidationAttribute), true)) | |
{ | |
if (!existingAttributes.Any(i => i.GetType() == attribute.GetType())) | |
yield return attribute; | |
} | |
} | |
} | |
} | |
} | |
if (existingAttributes != null) | |
{ | |
foreach (var attribute in existingAttributes) | |
{ | |
yield return attribute; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment