Skip to content

Instantly share code, notes, and snippets.

@jbogard
Created May 1, 2012 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbogard/2567888 to your computer and use it in GitHub Desktop.
Save jbogard/2567888 to your computer and use it in GitHub Desktop.
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