■ 열거형을 정렬하는 방법을 보여준다.
▶ EnumerationTypeConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
using System; using System.ComponentModel; using System.Globalization; using System.Linq; namespace TestProject { /// <summary> /// 열거형 타입 변환자 /// </summary> public class EnumerationTypeConverter : EnumConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - EnumerationTypeConverter(enumerationType) /// <summary> /// 생성자 /// </summary> /// <param name="enumerationType">열거형 타입</param> public EnumerationTypeConverter(Type enumerationType) : base(enumerationType) { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - ConvertTo(context, cultureInfo, value, targetType) /// <summary> /// 변환하기 /// </summary> /// <param name="context">타입 설명자 컨텍스트</param> /// <param name="cultureInfo">문화 정보</param> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <returns>설명</returns> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cultureInfo, object value, Type targetType) { if(targetType == typeof(string) && value != null) { Type enumerationType = value.GetType(); if(enumerationType.IsEnum) { return GetDescription(value); } } return base.ConvertTo(context, cultureInfo, value, targetType); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 설명 구하기 - GetDescription(value) /// <summary> /// 설명 구하기 /// </summary> /// <param name="value">값</param> /// <returns>설명</returns> private object GetDescription(object value) { DescriptionAttribute attribute = EnumType.GetField(value.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false) .FirstOrDefault() as DescriptionAttribute; if(attribute != null) { return attribute.Description; } return Enum.GetName(EnumType, value); } #endregion } } |
▶ Country.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 국가 /// </summary> [TypeConverter(typeof(EnumerationTypeConverter))] public enum Country : int { /// <summary> /// 호주 /// </summary> [Description("호주 (AUS)")] AUS = 0, /// <summary> /// 미국 /// </summary> [Description("미국 (USA)")] USA = 1, /// <summary> /// 영국 /// </summary> [Description("영국 (GBR)")] GBR = 2, /// <summary> /// 프랑스 /// </summary> [Description("프랑스 (FRA)")] FRA = 3, /// <summary> /// 독일 /// </summary> [Description("독일 (GER)")] GER = 4, /// <summary> /// 스페인 /// </summary> [Description("스페인 (ESP)")] ESP = 5, /// <summary> /// 자메이카 /// </summary> [Description("자메이카 (JAM)")] JAM = 6, /// <summary> /// 한국 /// </summary> [Description("한국 (KOR)")] KOR = 7, /// <summary> /// 우루과이 /// </summary> [Description("우루과이 (URU)")] URU = 8 } } |
▶ EnumerationHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace TestProject { /// <summary> /// 열거형 헬퍼 /// </summary> public class EnumerationHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 값으로 정렬하기 - SortByValue<TItem>(sourceList) /// <summary> /// 값으로 정렬하기 /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="sourceList">소스 리스트</param> /// <returns>값 정렬 리스트</returns> public static List<TItem> SortByValue<TItem>(List<TItem> sourceList) { List<string> itemList = new List<string>(); List<TItem> targetList = new List<TItem>(); foreach(TItem source in sourceList) { itemList.Add(source.ToString()); } itemList.Sort(); foreach(string item in itemList) { targetList.Add((TItem)Enum.Parse(typeof(TItem), item)); } return targetList; } #endregion #region 설명으로 정렬하기 - SortByDescription<TItem>(sourceList) /// <summary> /// 설명으로 정렬하기 /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="sourceList">소스 리스트</param> /// <returns>설명 정렬 리스트</returns> public static List<TItem> SortByDescription<TItem>(List<TItem> sourceList) { List<string> itemList = new List<string>(); List<TItem> targetList = new List<TItem>(); foreach(TItem source in sourceList) { itemList.Add(GetDescriptionFromValue<TItem>(source)); } itemList.Sort(); foreach(string item in itemList) { targetList.Add(GetValueFromDescription<TItem>(item)); } return targetList; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 값에서 설명 구하기 - GetDescriptionFromValue<TItem>(value) /// <summary> /// 값에서 설명 구하기 /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="value">값</param> /// <returns>설명</returns> private static string GetDescriptionFromValue<TItem>(TItem value) { DescriptionAttribute attribute = value.GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false) .SingleOrDefault() as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description; } #endregion #region 설명에서 값 구하기 - GetValueFromDescription<TItem>(description) /// <summary> /// 설명에서 값 구하기 /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="description">설명</param> /// <returns>값</returns> private static TItem GetValueFromDescription<TItem>(string description) { Type type = typeof(TItem); if(!type.IsEnum) { throw new ArgumentException(); } FieldInfo[] fieldInfoArray = type.GetFields(); var attributeInfo = fieldInfoArray.SelectMany ( f => f.GetCustomAttributes(typeof(DescriptionAttribute), false), (f, a) => new { Field = f, Attribute = a } ) .Where(a => ((DescriptionAttribute)a.Attribute).Description == description) .SingleOrDefault(); return attributeInfo == null ? default(TItem) : (TItem)attributeInfo.Field.GetRawConstantValue(); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="열거형 정렬하기" Background="#426aa1" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Label HorizontalAlignment="Left"> 열거형 값으로 정렬 : </Label> <ComboBox Width="300" Height="25" IsEditable="False" ItemsSource="{Binding Path=DefaultCountryList}" /> <Label HorizontalAlignment="Left" Margin="0 10 0 0"> 국가 코드로 정렬 : </Label> <ComboBox Width="300" Height="25" IsEditable="False" ItemsSource="{Binding Path=CountryListSortedByValue}" /> <Label HorizontalAlignment="Left" Margin="0 10 0 0"> 국가명으로 정렬 : </Label> <ComboBox Width="300" Height="25" IsEditable="False" ItemsSource="{Binding Path=CountryListSortedByDescription}" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
using System; using System.Collections.Generic; using System.Linq; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 디폴트 국가 리스트 - DefaultCountryList /// <summary> /// 디폴트 국가 리스트 /// </summary> public List<Country> DefaultCountryList { get; set; } #endregion #region 값으로 정렬 국가 리스트 - CountryListSortedByValue /// <summary> /// 값으로 정렬 국가 리스트 /// </summary> public List<Country> CountryListSortedByValue { get; set; } #endregion #region 설명으로 정렬 국가 리스트 - CountryListSortedByDescription /// <summary> /// 설명으로 정렬 국가 리스트 /// </summary> public List<Country> CountryListSortedByDescription { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); SetList(); DataContext = this; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 리스트 설정하기 - SetList() /// <summary> /// 리스트 설정하기 /// </summary> private void SetList() { List<Country> defaultCountryList = Enum.GetValues(typeof(Country)).Cast<Country>().ToList(); DefaultCountryList = defaultCountryList; CountryListSortedByValue = EnumerationHelper.SortByValue(defaultCountryList); CountryListSortedByDescription = EnumerationHelper.SortByDescription<Country>(defaultCountryList); } #endregion } } |