forked from sureshdr/mhora
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnumDescConverter.cs
134 lines (123 loc) · 4.59 KB
/
EnumDescConverter.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
133
134
/******
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******/
/*****************************************************************
* Module: EnumDescConverter.cs
* Type: C# Source Code
* Version: 1.0
* Description: Enum Converter using Description Attributes
*
* Revisions
* ------------------------------------------------
* [F] 24/02/2004, Jcl - Shaping up
* [B] 25/02/2004, Jcl - Made it much easier :-)
*
*****************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
namespace mhora
{
/// <summary>
/// EnumConverter supporting System.ComponentModel.DescriptionAttribute
/// </summary>
public class EnumDescConverter : System.ComponentModel.EnumConverter
{
protected System.Type myVal;
/// <summary>
/// Gets Enum Value's Description Attribute
/// </summary>
/// <param name="value">The value you want the description attribute for</param>
/// <returns>The description, if any, else it's .ToString()</returns>
public static string GetEnumDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
/// <summary>
/// Gets the description for certaing named value in an Enumeration
/// </summary>
/// <param name="value">The type of the Enumeration</param>
/// <param name="name">The name of the Enumeration value</param>
/// <returns>The description, if any, else the passed name</returns>
public static string GetEnumDescription(System.Type value, string name)
{
FieldInfo fi= value.GetField(name);
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:name;
}
/// <summary>
/// Gets the value of an Enum, based on it's Description Attribute or named value
/// </summary>
/// <param name="value">The Enum type</param>
/// <param name="description">The description or name of the element</param>
/// <returns>The value, or the passed in description, if it was not found</returns>
public static object GetEnumValue(System.Type value, string description)
{
FieldInfo [] fis = value.GetFields();
foreach(FieldInfo fi in fis)
{
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
if(attributes.Length>0)
{
if(attributes[0].Description == description)
{
return fi.GetValue(fi.Name);
}
}
if(fi.Name == description)
{
return fi.GetValue(fi.Name);
}
}
return description;
}
public EnumDescConverter(System.Type type) : base(type)
{
myVal = type;
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if(value is Enum && destinationType == typeof(string))
{
return GetEnumDescription((Enum)value);
}
if(value is string && destinationType == typeof(string))
{
return GetEnumDescription(myVal, (string)value);
}
return base.ConvertTo (context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
return GetEnumValue(myVal, (string)value);
}
if(value is Enum)
{
return GetEnumDescription((Enum)value);
}
return base.ConvertFrom (context, culture, value);
}
}
}