diff --git a/src/Humanizer.Tests.Shared/EnumHumanizeTests.cs b/src/Humanizer.Tests.Shared/EnumHumanizeTests.cs index 785ff4378..1fa438cff 100644 --- a/src/Humanizer.Tests.Shared/EnumHumanizeTests.cs +++ b/src/Humanizer.Tests.Shared/EnumHumanizeTests.cs @@ -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()); } + } } diff --git a/src/Humanizer.Tests.Shared/EnumUnderTest.cs b/src/Humanizer.Tests.Shared/EnumUnderTest.cs index 009f3dc56..041ddc0ac 100644 --- a/src/Humanizer.Tests.Shared/EnumUnderTest.cs +++ b/src/Humanizer.Tests.Shared/EnumUnderTest.cs @@ -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 @@ -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"; } } } diff --git a/src/Humanizer/EnumHumanizeExtensions.cs b/src/Humanizer/EnumHumanizeExtensions.cs index d7ce1e8bc..4aa89e8d2 100644 --- a/src/Humanizer/EnumHumanizeExtensions.cs +++ b/src/Humanizer/EnumHumanizeExtensions.cs @@ -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 StringTypedProperty = p => p.PropertyType == typeof(string); @@ -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)