Created
May 1, 2012 13:26
-
-
Save jbogard/2567888 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 class EnumerationTypeConvention : IPropertyConvention, IPropertyConventionAcceptance | |
{ | |
private static readonly Type OpenType = typeof(EnumerationType<>); | |
public void Apply(IPropertyInstance instance) | |
{ | |
var closedType = OpenType.MakeGenericType(instance.Property.PropertyType); | |
instance.CustomType(closedType); | |
} | |
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) | |
{ | |
criteria.Expect(x => IsEnumerationType(x.Property.PropertyType)); | |
} | |
private bool IsEnumerationType(Type type) | |
{ | |
return GetTypeHierarchy(type) | |
.Where(t => t.IsGenericType) | |
.Select(t => t.GetGenericTypeDefinition()) | |
.Any(t => t == typeof(Enumeration<>)); | |
} | |
private IEnumerable<Type> GetTypeHierarchy(Type type) | |
{ | |
while (type != null) | |
{ | |
yield return type; | |
type = type.BaseType; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment