Last change
on this file since 11850 was
11850,
checked in by bayral, 13 years ago
|
MAj des truc existant et debut du upload de fichier
|
File size:
1.4 KB
|
Line | |
---|
1 | using System; |
---|
2 | using System.Reflection; |
---|
3 | using System.Xml.Serialization; |
---|
4 | |
---|
5 | namespace Com.Piwigo.Lib.DTO.Helper |
---|
6 | { |
---|
7 | internal static class PwgEnumHelper<T> |
---|
8 | { |
---|
9 | static PwgEnumHelper() |
---|
10 | { |
---|
11 | if (!typeof(T).IsEnum) { |
---|
12 | throw new ArgumentException(String.Format("PwgEnumHelper<T>(): The type {0} is not a enum.", typeof(T).Name)); |
---|
13 | } |
---|
14 | } |
---|
15 | |
---|
16 | internal static String StringValueOf(T value) |
---|
17 | { |
---|
18 | FieldInfo fi = value.GetType().GetField(value.ToString()); |
---|
19 | XmlEnumAttribute[] attributes = (XmlEnumAttribute[])fi.GetCustomAttributes(typeof(XmlEnumAttribute), false); |
---|
20 | if (attributes.Length > 0) |
---|
21 | { |
---|
22 | return attributes[0].Name; |
---|
23 | } |
---|
24 | else |
---|
25 | { |
---|
26 | return value.ToString(); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | internal static T enumValueOf(String value) |
---|
31 | { |
---|
32 | Type enumType = typeof(T); |
---|
33 | Array values = Enum.GetValues(enumType); |
---|
34 | foreach (T enumValue in values) |
---|
35 | { |
---|
36 | if (StringValueOf(enumValue).Equals(value)) |
---|
37 | { |
---|
38 | return enumValue; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | throw new ArgumentException(String.Format("PwgEnumHelper<{0}>.enumValueOf({1}): The String is not a description or value of the specified enum.", typeof(T).Name, value)); |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.