Skip to content

Commit

Permalink
Merge pull request #608 from basildk/fix_enumNoDisplayAttrib
Browse files Browse the repository at this point in the history
605: Fix enum no display attrib
  • Loading branch information
Oren Novotny authored Apr 18, 2017
2 parents ef428a1 + 5508b9c commit 4b0ae7a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Humanizer.Tests.Shared/EnumHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ public void HonorsDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithDisplayAttribute, EnumUnderTest.MemberWithDisplayAttribute.Humanize());
}
[Fact]
public void HandlesDisplayAttributeWithNoDescription()
{
Assert.Equal(EnumTestsResources.MemberWithDisplayAttributeWithoutDescription, EnumUnderTest.MemberWithDisplayAttributeWithoutDescription.Humanize());
}

[Fact]
public void HonorsLocalizedDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithLocalizedDisplayAttribute, EnumUnderTest.MemberWithLocalizedDisplayAttribute.Humanize());
}

}
}
5 changes: 4 additions & 1 deletion src/Humanizer.Tests.Shared/EnumUnderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public enum EnumUnderTest
[Display(Description = EnumTestsResources.MemberWithDisplayAttribute)]
MemberWithDisplayAttribute,
[Display(Description = "MemberWithLocalizedDisplayAttribute", ResourceType = typeof(EnumTestsResources))]
MemberWithLocalizedDisplayAttribute
MemberWithLocalizedDisplayAttribute,
[Display(Name = EnumTestsResources.MemberWithDisplayAttributeWithoutDescription)]
MemberWithDisplayAttributeWithoutDescription
}

public class EnumTestsResources
Expand All @@ -37,6 +39,7 @@ public class EnumTestsResources
public const string MemberWithoutDescriptionAttributeTitle = "Member Without Description Attribute";
public const string MemberWithoutDescriptionAttributeLowerCase = "member without description attribute";
public const string MemberWithDisplayAttribute = "Description from Display attribute";
public const string MemberWithDisplayAttributeWithoutDescription = "Displayattribute without description";
public static string MemberWithLocalizedDisplayAttribute { get { return "Localized description from Display attribute"; } }
}

Expand Down
18 changes: 15 additions & 3 deletions src/Humanizer/EnumHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static class EnumHumanizeExtensions
{
private const string DisplayAttributeTypeName = "System.ComponentModel.DataAnnotations.DisplayAttribute";
private const string DisplayAttributeGetDescriptionMethodName = "GetDescription";
private const string DisplayAttributeGetNameMethodName = "GetName";

private static readonly Func<PropertyInfo, bool> StringTypedProperty = p => p.PropertyType == typeof(string);

Expand Down Expand Up @@ -69,10 +70,21 @@ private static string GetCustomDescription(MemberInfo memberInfo)
var attrType = attr.GetType();
if (attrType.FullName == DisplayAttributeTypeName)
{
var method = attrType.GetRuntimeMethod(DisplayAttributeGetDescriptionMethodName, new Type[0]);
if (method != null)
return method.Invoke(attr, new object[0]).ToString();
var methodGetDescription = attrType.GetRuntimeMethod(DisplayAttributeGetDescriptionMethodName, new Type[0]);
if (methodGetDescription != null)
{
var executedMethod = methodGetDescription.Invoke(attr, new object[0]);
if (executedMethod != null) return executedMethod.ToString();
}
var methodGetName = attrType.GetRuntimeMethod(DisplayAttributeGetNameMethodName, new Type[0]);
if (methodGetName != null)
{
var executedMethod = methodGetName.Invoke(attr, new object[0]);
if (executedMethod != null) return executedMethod.ToString();
}
return null;
}

var descriptionProperty =
attrType.GetRuntimeProperties()
.Where(StringTypedProperty)
Expand Down

0 comments on commit 4b0ae7a

Please sign in to comment.