Created
March 5, 2021 07:27
-
-
Save theilgaz/9dbd5834a86dd0c4248f0baccb762539 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.ComponentModel.DataAnnotations; | |
using System.Globalization; | |
using System.Linq; | |
using System.Reflection; | |
namespace theilgazcode.Utilities | |
{ | |
public class EnumItem | |
{ | |
public int Value { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} | |
public static class EnumHelper | |
{ | |
/// <summary> | |
/// Enum değerine ait açıklama bilgisi döner. | |
/// </summary> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static string GetDescription(Enum value) | |
{ | |
return | |
value | |
.GetType() | |
.GetMember(value.ToString()) | |
.FirstOrDefault() | |
?.GetCustomAttribute<DescriptionAttribute>() | |
?.Description | |
?? value.ToString(); | |
} | |
/// <summary> | |
/// Value ve Description değerleri SortedDictionary olarak döner. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <returns></returns> | |
public static SortedDictionary<int, string> GetValues<T>() where T : struct, IConvertible | |
{ | |
if (!typeof(T).IsEnum) | |
{ | |
throw new ArgumentException("Enum tipi gönderilmeli."); | |
} | |
SortedDictionary<int, string> sortedList = new SortedDictionary<int, string>(); | |
Type enumType = typeof(T); | |
foreach (T value in Enum.GetValues(enumType)) | |
{ | |
FieldInfo fi = enumType.GetField(value.ToString()); | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (attributes != null && attributes.Length > 0) | |
{ | |
DescriptionAttribute description = (DescriptionAttribute)attributes[0]; | |
sortedList.Add(value.ToInt32(CultureInfo.CurrentCulture.NumberFormat), description.Description); | |
} | |
else | |
{ | |
sortedList.Add(value.ToInt32(CultureInfo.CurrentCulture.NumberFormat), value.ToString()); | |
} | |
} | |
return sortedList; | |
} | |
/// <summary> | |
/// Enum değeri parametre geçilerek enum olarak geri döndürür. | |
/// | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static T GetEnum<T>(int value) where T : struct, IConvertible | |
{ | |
if (!typeof(T).IsEnum) | |
{ | |
throw new ArgumentException("Enum tipi gönderilmeli."); | |
} | |
Type enumType = typeof(T); | |
var values = Enum.GetValues(enumType); | |
foreach (T e in values) | |
{ | |
if (e.ToInt32(CultureInfo.CurrentCulture.NumberFormat) == value) | |
{ | |
return e; | |
} | |
} | |
return new T(); | |
} | |
/// <summary> | |
/// Enum ismi parametre geçilerek enum olarak geri döndürür. | |
/// | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="name"></param> | |
/// <returns></returns> | |
public static T GetEnum<T>(string name) where T : struct, IConvertible | |
{ | |
if (!typeof(T).IsEnum) | |
{ | |
throw new ArgumentException("Enum tipi gönderilmeli."); | |
} | |
Type enumType = typeof(T); | |
var values = Enum.GetValues(enumType); | |
foreach (T e in values) | |
{ | |
FieldInfo fi = enumType.GetField(e.ToString()); | |
if (fi.Name == name) | |
{ | |
return e; | |
} | |
} | |
return new T(); | |
} | |
/// <summary> | |
/// Enum değeri parametre geçilerek EnumItem olarak geri döndürür. | |
/// | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static EnumItem GetItem<T>(int value) where T:struct,IConvertible | |
{ | |
if (!typeof(T).IsEnum) | |
{ | |
throw new ArgumentException("Enum tipi gönderilmeli."); | |
} | |
Type enumType = typeof(T); | |
foreach (T e in Enum.GetValues(enumType)) | |
{ | |
if (e.ToInt32(CultureInfo.CurrentCulture.NumberFormat) == value) | |
{ | |
FieldInfo fi = enumType.GetField(e.ToString()); | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (attributes != null && attributes.Length > 0) | |
{ | |
DescriptionAttribute description = (DescriptionAttribute)attributes[0]; | |
return new EnumItem() | |
{ | |
Value = value, | |
Description = description.Description, | |
Name = fi.Name | |
}; | |
} | |
else | |
{ | |
return new EnumItem() | |
{ | |
Value = value, | |
Description = value.ToString(), | |
Name = fi.Name | |
}; | |
} | |
} | |
} | |
return new EnumItem() | |
{ | |
Value = -1, | |
Description = "Enum Değeri Bulunamadı", | |
}; | |
} | |
/// <summary> | |
/// Enum ismi parametre geçilerek EnumItem olarak geri döndürür. | |
/// | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="name"></param> | |
/// <returns></returns> | |
public static EnumItem GetItem<T>(string name) where T : struct, IConvertible | |
{ | |
if (!typeof(T).IsEnum) | |
{ | |
throw new ArgumentException("Enum tipi gönderilmeli."); | |
} | |
Type enumType = typeof(T); | |
foreach (T e in Enum.GetValues(enumType)) | |
{ | |
FieldInfo fi = enumType.GetField(e.ToString()); | |
if (fi.Name == name) | |
{ | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (attributes != null && attributes.Length > 0) | |
{ | |
DescriptionAttribute description = (DescriptionAttribute)attributes[0]; | |
return new EnumItem() | |
{ | |
Value = e.ToInt32(CultureInfo.CurrentCulture.NumberFormat), | |
Description = description.Description, | |
Name = fi.Name | |
}; | |
} | |
else | |
{ | |
return new EnumItem() | |
{ | |
Value = e.ToInt32(CultureInfo.CurrentCulture.NumberFormat), | |
Description = name, | |
Name = fi.Name | |
}; | |
} | |
} | |
} | |
return new EnumItem() | |
{ | |
Value = -1, | |
Description = "Enum Değeri Bulunamadı", | |
}; | |
} | |
/// <summary> | |
/// Enum tipine tanımlı tüm değerleri EnumItem listesi olarak döndürür. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <returns></returns> | |
public static List<EnumItem> GetList<T>() where T: struct, IConvertible | |
{ | |
if (!typeof(T).IsEnum) | |
{ | |
throw new ArgumentException("Enum tipi gönderilmeli."); | |
} | |
List<EnumItem> list = new List<EnumItem>(); | |
Type enumType = typeof(T); | |
foreach (T value in Enum.GetValues(enumType)) | |
{ | |
FieldInfo fi = enumType.GetField(value.ToString()); | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (attributes != null && attributes.Length > 0) | |
{ | |
DescriptionAttribute description = (DescriptionAttribute)attributes[0]; | |
list.Add( | |
new EnumItem() { | |
Value = value.ToInt32(CultureInfo.CurrentCulture.NumberFormat), | |
Description = description.Description, | |
Name = fi.Name | |
}); | |
} | |
else | |
{ | |
list.Add( | |
new EnumItem() { | |
Value = value.ToInt32(CultureInfo.CurrentCulture.NumberFormat), | |
Description = value.ToString(), | |
Name = fi.Name | |
}); | |
} | |
} | |
return list; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment