Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Add flag enum support #9

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ Otherwise the method will return the field name of the enum.

```c#
var displayName = Enum.EnumValue.GetDisplayName();
```

## EnumExtensions.GetFlagsDisplayName()

If the EnumDisplayNameAttribute had been applied to an flag enum this method returns the display names set using this attribute in a comma seperated string.

If the System.ComponentModel.DataAnnotations.DisplayAttribute has been applied to an enum, and the EnumDisplayNameAttribute has not been applied, this method returns the display name set using this attribute.

Otherwise the method will return the field name of the enum.

```c#
var displayName = (Enum.EnumValue1 | Enum.EnumValue2).GetFlagsDisplayName();
```
85 changes: 74 additions & 11 deletions src/EnumDisplayName/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

#if NETSTANDARD1_3
using System.Reflection;
Expand Down Expand Up @@ -35,26 +37,87 @@ public static string GetDisplayName(this Enum enumValue)

if (string.Compare(fieldName, field.Name, StringComparison.Ordinal) == 0)
{
var customAttributes = field.GetCustomAttributes(false);
var enumDisplayNameAttribute = customAttributes.OfType<EnumDisplayNameAttribute>().FirstOrDefault();
var fieldAttributes = field
.GetCustomAttributes(false)
.Cast<Attribute>();

if (enumDisplayNameAttribute != null)
{
return enumDisplayNameAttribute.GetDisplayName();
}
return GetNameFromEnumField(enumValue, fieldAttributes);
}
}

return string.Empty;
}

/// <summary>
/// Returns the display names of a flag enum as a comma seperated string as specified by the
/// EnumDisplayNameAttribute declared on the enum field. If no EnumDisplayNameAttribute is
/// declared on the enum field then the name of the enum field is returned.
/// </summary>
public static string GetFlagsDisplayName(this Enum enumValue)
{
if (enumValue == null)
{
return string.Empty;
}

var displayNameAttribute = customAttributes.OfType<DisplayAttribute>().FirstOrDefault();
var enumType = enumValue.GetType();
var fieldNames = Enum.GetNames(enumType);
var fields = enumType.GetFields()
.Where(o => fieldNames.Contains(o.Name, StringComparer.Ordinal))
.ToArray();

if (displayNameAttribute != null)
var fieldPosition = 0;
var result = new StringBuilder();
foreach (Enum currentValue in Enum.GetValues(enumType))
{
if (enumValue.HasFlag(currentValue))
{
var currentField = fields[fieldPosition];
if (currentField.IsSpecialName)
{
return displayNameAttribute.GetName();
fieldPosition += 1;
continue;
}

return fieldName;
var fieldAttributes = currentField
.GetCustomAttributes(false)
.Cast<Attribute>();

var fieldName = GetNameFromEnumField(enumValue, fieldAttributes);

var resultPartToAdd = result.Length == 0 ? fieldName : "," + fieldName;
result.Append(resultPartToAdd);
}

fieldPosition += 1;
}

return string.Empty;
return result.ToString();
}

private static string GetNameFromEnumField(Enum enumValue, IEnumerable<Attribute> fieldAttributes)
{
var enumType = enumValue.GetType();
var fieldName = Enum.GetName(enumType, enumValue);
var enumDisplayNameAttribute = fieldAttributes
.OfType<EnumDisplayNameAttribute>()
.FirstOrDefault();

if (enumDisplayNameAttribute != null)
{
return enumDisplayNameAttribute.GetDisplayName();
}

var displayNameAttribute = fieldAttributes
.OfType<DisplayAttribute>()
.FirstOrDefault();

if (displayNameAttribute != null)
{
return displayNameAttribute.GetName();
}

return fieldName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using Xunit;

namespace Enable.EnumDisplayName
{
public class EnumExtensionsGetFlagsDisplayNameTests
{
[Fact]
public void GetFlagsDisplayName_ReturnsEmptyStringWhenSourceIsNull()
{
// Arrange
Enum source = null;

// Act
var result = source.GetFlagsDisplayName();

// Assert
Assert.Equal(string.Empty, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsEnumDisplayNameAttributeDisplayName()
{
// Act
var result = MockFlagEnum.EnumDisplayNameAttribute.GetFlagsDisplayName();

// Assert
Assert.Equal(MockEnumDisplayNames.EnumDisplayNameAttribute, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsEnumDisplayNameAttributeWithResourceDisplayName()
{
// Act
var result = MockFlagEnum.EnumDisplayNameAttributeWithResource.GetFlagsDisplayName();

// Assert
Assert.Equal(Resources.MockEnum.EnumDisplayNameAttributeWithResource, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsDisplayAttributeDisplayName()
{
// Act
var result = MockFlagEnum.DisplayAttribute.GetFlagsDisplayName();

// Assert
Assert.Equal(MockEnumDisplayNames.DisplayAttribute, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsFieldName()
{
// Act
var result = MockFlagEnum.NoAttribute.GetFlagsDisplayName();

// Assert
Assert.Equal(MockEnumDisplayNames.NoAttribute, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsEmptyStringWithSpecialNameAttribute()
{
// Act
var result = MockFlagEnum.SpecialName.GetFlagsDisplayName();

// Assert
Assert.Equal(string.Empty, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsCommaSeperatedStringForFlagValues()
{
// Arrange
var sut = MockFlagEnum.DisplayAttribute | MockFlagEnum.EnumDisplayNameAttribute;

// Act
var result = sut.GetFlagsDisplayName();

// Assert
var expected = $"{MockEnumDisplayNames.EnumDisplayNameAttribute},{MockEnumDisplayNames.DisplayAttribute}";
Assert.Equal(expected, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsCommaSeperatedStringForFlagValues_ExceptSpecialNameAttributes()
{
// Arrange
var sut = MockFlagEnum.DisplayAttribute | MockFlagEnum.SpecialName;

// Act
var result = sut.GetFlagsDisplayName();

// Assert
var expected = $"{MockEnumDisplayNames.DisplayAttribute}";
Assert.Equal(expected, result);
}
}
}
25 changes: 25 additions & 0 deletions test/EnumDisplayName.Test/Enums/MockFlagEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace Enable.EnumDisplayName
{
[Flags]
public enum MockFlagEnum
{
[EnumDisplayName(MockEnumDisplayNames.EnumDisplayNameAttribute)]
EnumDisplayNameAttribute = 1,

[EnumDisplayName(MockEnumDisplayNames.EnumDisplayNameAttributeWithResource, typeof(Resources.MockEnum))]
EnumDisplayNameAttributeWithResource = 2,

[Display(Name = MockEnumDisplayNames.DisplayAttribute)]
DisplayAttribute = 4,

NoAttribute = 8,

[SpecialName]
[EnumDisplayName(MockEnumDisplayNames.SpecialName)]
SpecialName = 16
}
}