public
Created

  • Download Gist
EnumerationTypeConvention.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.