wasSharp – Diff between revs 3 and 5
?pathlinks?
Rev 3 | Rev 5 | |||
---|---|---|---|---|
Line 18... | Line 18... | |||
18 | /////////////////////////////////////////////////////////////////////////// |
18 | /////////////////////////////////////////////////////////////////////////// |
|
19 | /// <summary> |
19 | /// <summary> |
|
20 | /// Retrieves an attribute of type T from an enumeration. |
20 | /// Retrieves an attribute of type T from an enumeration. |
|
21 | /// </summary> |
21 | /// </summary> |
|
22 | /// <returns>an attribute of type T</returns> |
22 | /// <returns>an attribute of type T</returns> |
|
23 | public static T wasGetAttributeFromEnumValue<T>(Enum value) where T : Attribute |
23 | public static T GetAttributeFromEnumValue<T>(Enum value) where T : Attribute |
|
24 | { |
24 | { |
|
25 | return (T) value.GetType() |
25 | return (T) value.GetType() |
|
26 | .GetRuntimeField(value.ToString()) |
26 | .GetRuntimeField(value.ToString()) |
|
27 | .GetCustomAttributes(typeof (T), false) |
27 | .GetCustomAttributes(typeof (T), false) |
|
28 | .SingleOrDefault(); |
28 | .SingleOrDefault(); |
|
Line 34... | Line 34... | |||
34 | /// <summary> |
34 | /// <summary> |
|
35 | /// Returns all the attributes of type T of an enumeration. |
35 | /// Returns all the attributes of type T of an enumeration. |
|
36 | /// </summary> |
36 | /// </summary> |
|
37 | /// <typeparam name="T">the attribute to retrieve</typeparam> |
37 | /// <typeparam name="T">the attribute to retrieve</typeparam> |
|
38 | /// <returns>a list of attributes</returns> |
38 | /// <returns>a list of attributes</returns> |
|
39 | public static IEnumerable<T> wasGetEnumAttributes<T>(Enum e) where T : Attribute |
39 | public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute |
|
40 | { |
40 | { |
|
41 | return e.GetType().GetRuntimeFields() |
41 | return e.GetType().GetRuntimeFields() |
|
42 | .AsParallel() |
42 | .AsParallel() |
|
43 | .Select(o => wasGetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>()))); |
43 | .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>()))); |
|
44 | } |
44 | } |
|
Line 45... | Line 45... | |||
45 | |
45 | |
|
46 | /////////////////////////////////////////////////////////////////////////// |
46 | /////////////////////////////////////////////////////////////////////////// |
|
47 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
47 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
48 | /////////////////////////////////////////////////////////////////////////// |
48 | /////////////////////////////////////////////////////////////////////////// |
|
49 | /// <summary> |
49 | /// <summary> |
|
50 | /// Returns all the field names of an enumeration. |
50 | /// Returns all the field names of an enumeration. |
|
51 | /// </summary> |
51 | /// </summary> |
|
52 | /// <returns>the field names</returns> |
52 | /// <returns>the field names</returns> |
|
53 | public static IEnumerable<string> wasGetEnumNames<T>() |
53 | public static IEnumerable<string> GetEnumNames<T>() |
|
54 | { |
54 | { |
|
55 | return |
55 | return |
|
56 | typeof (T).GetRuntimeFields() |
56 | typeof (T).GetRuntimeFields() |
|
57 | .AsParallel() |
57 | .AsParallel() |
|
Line 66... | Line 66... | |||
66 | /////////////////////////////////////////////////////////////////////////// |
66 | /////////////////////////////////////////////////////////////////////////// |
|
67 | /// <summary> |
67 | /// <summary> |
|
68 | /// Returns all the values of an enumeration. |
68 | /// Returns all the values of an enumeration. |
|
69 | /// </summary> |
69 | /// </summary> |
|
70 | /// <returns>the values of the enumeration</returns> |
70 | /// <returns>the values of the enumeration</returns> |
|
71 | public static IEnumerable<T> wasGetEnumValues<T>() |
71 | public static IEnumerable<T> GetEnumValues<T>() |
|
72 | { |
72 | { |
|
73 | return Enum.GetValues(typeof (T)).Cast<object>().Select(value => (T) value); |
73 | return Enum.GetValues(typeof (T)).Cast<object>().Select(value => (T) value); |
|
74 | } |
74 | } |
|
Line 75... | Line 75... | |||
75 | |
75 | |
|
Line 79... | Line 79... | |||
79 | /// <summary> |
79 | /// <summary> |
|
80 | /// Get the description from an enumeration value. |
80 | /// Get the description from an enumeration value. |
|
81 | /// </summary> |
81 | /// </summary> |
|
82 | /// <param name="value">an enumeration value</param> |
82 | /// <param name="value">an enumeration value</param> |
|
83 | /// <returns>the description or the empty string</returns> |
83 | /// <returns>the description or the empty string</returns> |
|
84 | public static string wasGetNameFromEnumValue(Enum value) |
84 | public static string GetNameFromEnumValue(Enum value) |
|
85 | { |
85 | { |
|
86 | NameAttribute attribute = value.GetType() |
86 | NameAttribute attribute = value.GetType() |
|
87 | .GetRuntimeField(value.ToString()) |
87 | .GetRuntimeField(value.ToString()) |
|
88 | .GetCustomAttributes(typeof (NameAttribute), false) |
88 | .GetCustomAttributes(typeof (NameAttribute), false) |
|
89 | .SingleOrDefault() as NameAttribute; |
89 | .SingleOrDefault() as NameAttribute; |
|
Line 96... | Line 96... | |||
96 | /// <summary> |
96 | /// <summary> |
|
97 | /// Get the description from an enumeration value. |
97 | /// Get the description from an enumeration value. |
|
98 | /// </summary> |
98 | /// </summary> |
|
99 | /// <param name="value">an enumeration value</param> |
99 | /// <param name="value">an enumeration value</param> |
|
100 | /// <returns>the description or the empty string</returns> |
100 | /// <returns>the description or the empty string</returns> |
|
101 | public static string wasGetDescriptionFromEnumValue(Enum value) |
101 | public static string GetDescriptionFromEnumValue(Enum value) |
|
102 | { |
102 | { |
|
103 | DescriptionAttribute attribute = value.GetType() |
103 | DescriptionAttribute attribute = value.GetType() |
|
104 | .GetRuntimeField(value.ToString()) |
104 | .GetRuntimeField(value.ToString()) |
|
105 | .GetCustomAttributes(typeof (DescriptionAttribute), false) |
105 | .GetCustomAttributes(typeof (DescriptionAttribute), false) |
|
106 | .SingleOrDefault() as DescriptionAttribute; |
106 | .SingleOrDefault() as DescriptionAttribute; |
|
Line 114... | Line 114... | |||
114 | /// Get enumeration value from its name attribute. |
114 | /// Get enumeration value from its name attribute. |
|
115 | /// </summary> |
115 | /// </summary> |
|
116 | /// <typeparam name="T">the enumeration type</typeparam> |
116 | /// <typeparam name="T">the enumeration type</typeparam> |
|
117 | /// <param name="name">the description of a member</param> |
117 | /// <param name="name">the description of a member</param> |
|
118 | /// <returns>the value or the default of T if case no name attribute found</returns> |
118 | /// <returns>the value or the default of T if case no name attribute found</returns> |
|
119 | public static T wasGetEnumValueFromName<T>(string name) |
119 | public static T GetEnumValueFromName<T>(string name) |
|
120 | { |
120 | { |
|
121 | var field = typeof (T).GetRuntimeFields() |
121 | var field = typeof (T).GetRuntimeFields() |
|
122 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
122 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
|
123 | typeof (NameAttribute), false), ( |
123 | typeof (NameAttribute), false), ( |
|
124 | f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att) |
124 | f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att) |
|
Line 134... | Line 134... | |||
134 | /// </summary> |
134 | /// </summary> |
|
135 | /// <typeparam name="T">the type of the structure to search</typeparam> |
135 | /// <typeparam name="T">the type of the structure to search</typeparam> |
|
136 | /// <param name="structure">the structure to search</param> |
136 | /// <param name="structure">the structure to search</param> |
|
137 | /// <param name="item">the value of the item to search</param> |
137 | /// <param name="item">the value of the item to search</param> |
|
138 | /// <returns>the description or the empty string</returns> |
138 | /// <returns>the description or the empty string</returns> |
|
139 | public static string wasGetStructureMemberName<T>(T structure, object item) where T : struct |
139 | public static string GetStructureMemberName<T>(T structure, object item) where T : struct |
|
140 | { |
140 | { |
|
141 | var field = typeof (T).GetRuntimeFields() |
141 | var field = typeof (T).GetRuntimeFields() |
|
142 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false), |
142 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false), |
|
143 | (f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item)); |
143 | (f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item)); |
|
144 | return field != null ? ((NameAttribute) field.Att).Name : string.Empty; |
144 | return field != null ? ((NameAttribute) field.Att).Name : string.Empty; |