...
This commit is contained in:
24
Blazor.PersianDatePicker/Extensions/EnumExtensions.cs
Normal file
24
Blazor.PersianDatePicker/Extensions/EnumExtensions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Blazor.PersianDatePicker.Extensions
|
||||
{
|
||||
public static class EnumExtensions
|
||||
{
|
||||
public static string? ToEnumDisplayName(this Enum value, bool showEnumStringIfNoDisplayName = true)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
|
||||
var displayName = value.GetType()
|
||||
.GetMember(value.ToString())
|
||||
.First()
|
||||
?.GetCustomAttributes<DisplayAttribute>()
|
||||
.FirstOrDefault()
|
||||
?.GetName();
|
||||
return displayName ?? (showEnumStringIfNoDisplayName ? value.ToString() : null);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user