-
-
Save borakasmer/0f38dd4a3b2b52535544254038ff2a77 to your computer and use it in GitHub Desktop.
This file contains 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.DataAnnotations; | |
using System.Linq; | |
using System.Reflection; | |
namespace StockList | |
{ | |
[Flags] | |
public enum WeaponType : byte | |
{ | |
Axe = 1, | |
Arrow = 2, | |
Pistol = 4, | |
Stick = 8, | |
Sword = 16, | |
Rifle = 32 | |
} | |
enum SalesPriceType : byte | |
{ | |
[Display(Name = "Nakit Prk.Fiyat")] | |
NakitPrkFiyat = 1, | |
[Display(Name = "2.Prk. Fiyatı")] | |
PrkFiyatı = 2, | |
[Display(Name = "Nakit Topt.Fiyatı")] | |
NakitToptFiyatı = 3, | |
[Display(Name = "1.Satış Fiyatı")] | |
BirinciSatışFiyatı = 4, | |
[Display(Name = "2.Satış Fiyatı")] | |
İkinciSatışFiyatı = 5, | |
[Display(Name = "3.Satış Fiyatı")] | |
ÜcüncüSatışFiyatı = 6, | |
[Display(Name = "4.Satış Fiyatı")] | |
DördüncüSatışFiyatı = 7 | |
} | |
public class EnumModel | |
{ | |
public int Value { get; set; } | |
public string Name { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
WeaponType boraAttacker = WeaponType.Axe | WeaponType.Rifle; | |
Console.WriteLine(boraAttacker); | |
Console.WriteLine((int)boraAttacker); | |
Console.WriteLine(boraAttacker.HasFlag(WeaponType.Axe)); | |
GetEnumList<WeaponType>().ForEach(weapon => | |
{ | |
if (boraAttacker.HasFlag((WeaponType)weapon.Value)) { Console.WriteLine($"Bora's Weapon: { weapon.Key}"); } | |
}); | |
//Console.WriteLine(WeaponType.Stick | WeaponType.Sword); | |
//Console.WriteLine(WeaponType.Axe | WeaponType.Pistol); | |
//Console.WriteLine($"Satış Nakit Toplam Fiyat Tipi:", SalesPriceType.NakitToptFiyatı); | |
Console.WriteLine(SalesPriceType.NakitToptFiyatı.GetDisplayName()); | |
Console.WriteLine(((SalesPriceType)2).GetDisplayName()); | |
Console.WriteLine(((SalesPriceType)6).GetAttribute<DisplayAttribute>().Name); | |
foreach (EnumModel model in GetListOfEnum()) | |
{ | |
Console.WriteLine($"Enum Name:{model.Name} - Enum Value:{model.Value}"); | |
} | |
foreach (KeyValuePair<string, byte> model in GetEnumList<SalesPriceType>()) | |
{ | |
Console.WriteLine($"[DisplayName]:{model.Key} - [Value]:{model.Value}"); | |
} | |
} | |
static List<EnumModel> GetListOfEnum() | |
{ | |
return ((SalesPriceType[])Enum.GetValues(typeof(SalesPriceType))).Select(c => new EnumModel() { Value = (int)c, Name = c.GetDisplayName() }).ToList(); | |
} | |
public static List<KeyValuePair<string, byte>> GetEnumList<T>() | |
{ | |
var list = new List<KeyValuePair<string, byte>>(); | |
foreach (var e in Enum.GetValues(typeof(T))) | |
{ | |
list.Add(new KeyValuePair<string, byte>(((Enum)e).GetDisplayName(), (byte)e)); | |
} | |
return list; | |
} | |
} | |
public static class EnumExtensions | |
{ | |
public static string GetDisplayName(this Enum enu) | |
{ | |
var attr = GetDisplayAttribute(enu); | |
return attr != null ? attr.Name : enu.ToString(); | |
} | |
public static string GetDescription(this Enum enu) | |
{ | |
var attr = GetDisplayAttribute(enu); | |
return attr != null ? attr.Description : enu.ToString(); | |
} | |
private static DisplayAttribute GetDisplayAttribute(object value) | |
{ | |
Type type = value.GetType(); | |
if (!type.IsEnum) | |
{ | |
throw new ArgumentException(string.Format("Type {0} is not an enum", type)); | |
} | |
// Get the enum field. | |
var field = type.GetField(value.ToString()); | |
return field == null ? null : field.GetCustomAttribute<DisplayAttribute>(); | |
} | |
} | |
public static class EnumExtensions2 | |
{ | |
public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) | |
where TAttribute : Attribute | |
{ | |
return enumValue.GetType() | |
.GetMember(enumValue.ToString()) | |
.First() | |
.GetCustomAttribute<TAttribute>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment