You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Mesnac.PlcUtils.common
|
|
{
|
|
public static class EnumExtensions
|
|
{
|
|
public static string GetDescription<T>(this T enumerationValue) where T : struct
|
|
{
|
|
var type = enumerationValue.GetType();
|
|
if (!type.IsEnum)
|
|
{
|
|
throw new ArgumentException($"{nameof(enumerationValue)} must be of Enum type", nameof(enumerationValue));
|
|
}
|
|
|
|
var memberInfo = type.GetMember(enumerationValue.ToString());
|
|
if (memberInfo.Length > 0)
|
|
{
|
|
var descriptionAttribute = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
|
|
|
|
if (descriptionAttribute != null && descriptionAttribute.Length > 0)
|
|
{
|
|
return descriptionAttribute[0].Description;
|
|
}
|
|
}
|
|
|
|
return enumerationValue.ToString();
|
|
}
|
|
}
|
|
}
|