corrade-vassal – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
13 | eva | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Linq; |
||
10 | using System.Reflection; |
||
11 | |||
12 | namespace wasSharp |
||
13 | { |
||
16 | eva | 14 | public static class Reflection |
13 | eva | 15 | { |
16 | /////////////////////////////////////////////////////////////////////////// |
||
17 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
18 | /////////////////////////////////////////////////////////////////////////// |
||
19 | /// <summary> |
||
20 | /// Retrieves an attribute of type T from an enumeration. |
||
21 | /// </summary> |
||
22 | /// <returns>an attribute of type T</returns> |
||
23 | public static T GetAttributeFromEnumValue<T>(Enum value) where T : Attribute |
||
24 | { |
||
25 | return (T) value.GetType() |
||
26 | .GetRuntimeField(value.ToString()) |
||
27 | .GetCustomAttributes(typeof (T), false) |
||
28 | .SingleOrDefault(); |
||
29 | } |
||
30 | |||
31 | /////////////////////////////////////////////////////////////////////////// |
||
32 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
33 | /////////////////////////////////////////////////////////////////////////// |
||
34 | /// <summary> |
||
35 | /// Returns all the attributes of type T of an enumeration. |
||
36 | /// </summary> |
||
37 | /// <typeparam name="T">the attribute to retrieve</typeparam> |
||
38 | /// <returns>a list of attributes</returns> |
||
39 | public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute |
||
40 | { |
||
16 | eva | 41 | return e.GetType().GetRuntimeFields().ToArray() |
13 | eva | 42 | .AsParallel() |
43 | .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>()))); |
||
44 | } |
||
45 | |||
46 | /////////////////////////////////////////////////////////////////////////// |
||
47 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
48 | /////////////////////////////////////////////////////////////////////////// |
||
49 | /// <summary> |
||
50 | /// Returns all the field names of an enumeration. |
||
51 | /// </summary> |
||
52 | /// <returns>the field names</returns> |
||
53 | public static IEnumerable<string> GetEnumNames<T>() |
||
54 | { |
||
55 | return |
||
16 | eva | 56 | typeof (T).GetRuntimeFields().ToArray() |
13 | eva | 57 | .AsParallel() |
58 | .Select(o => o.GetCustomAttribute(typeof (NameAttribute), false)) |
||
59 | .Select(o => (o as NameAttribute)?.Name) |
||
60 | .Where(o => !string.IsNullOrEmpty(o)) |
||
61 | .Select(o => o); |
||
62 | } |
||
63 | |||
64 | /////////////////////////////////////////////////////////////////////////// |
||
65 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
66 | /////////////////////////////////////////////////////////////////////////// |
||
67 | /// <summary> |
||
68 | /// Returns all the values of an enumeration. |
||
69 | /// </summary> |
||
70 | /// <returns>the values of the enumeration</returns> |
||
71 | public static IEnumerable<T> GetEnumValues<T>() |
||
72 | { |
||
73 | return Enum.GetValues(typeof (T)).Cast<object>().Select(value => (T) value); |
||
74 | } |
||
75 | |||
76 | /////////////////////////////////////////////////////////////////////////// |
||
77 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
78 | /////////////////////////////////////////////////////////////////////////// |
||
79 | /// <summary> |
||
80 | /// Get the description from an enumeration value. |
||
81 | /// </summary> |
||
82 | /// <param name="value">an enumeration value</param> |
||
83 | /// <returns>the description or the empty string</returns> |
||
84 | public static string GetNameFromEnumValue(Enum value) |
||
85 | { |
||
16 | eva | 86 | var attribute = value.GetType() |
13 | eva | 87 | .GetRuntimeField(value.ToString()) |
88 | .GetCustomAttributes(typeof (NameAttribute), false) |
||
89 | .SingleOrDefault() as NameAttribute; |
||
90 | return attribute?.Name; |
||
91 | } |
||
92 | |||
93 | /////////////////////////////////////////////////////////////////////////// |
||
94 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
95 | /////////////////////////////////////////////////////////////////////////// |
||
96 | /// <summary> |
||
97 | /// Get the description from an enumeration value. |
||
98 | /// </summary> |
||
99 | /// <param name="value">an enumeration value</param> |
||
100 | /// <returns>the description or the empty string</returns> |
||
101 | public static string GetDescriptionFromEnumValue(Enum value) |
||
102 | { |
||
16 | eva | 103 | var attribute = value.GetType() |
13 | eva | 104 | .GetRuntimeField(value.ToString()) |
105 | .GetCustomAttributes(typeof (DescriptionAttribute), false) |
||
106 | .SingleOrDefault() as DescriptionAttribute; |
||
107 | return attribute?.Description; |
||
108 | } |
||
109 | |||
110 | /////////////////////////////////////////////////////////////////////////// |
||
111 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
112 | /////////////////////////////////////////////////////////////////////////// |
||
113 | /// <summary> |
||
114 | /// Get enumeration value from its name attribute. |
||
115 | /// </summary> |
||
116 | /// <typeparam name="T">the enumeration type</typeparam> |
||
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> |
||
119 | public static T GetEnumValueFromName<T>(string name) |
||
120 | { |
||
16 | eva | 121 | var field = typeof (T).GetRuntimeFields().ToArray() |
13 | eva | 122 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
123 | typeof (NameAttribute), false), ( |
||
124 | f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att) |
||
125 | .Name.Equals(name)); |
||
126 | return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
||
127 | } |
||
128 | |||
129 | /////////////////////////////////////////////////////////////////////////// |
||
130 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
131 | /////////////////////////////////////////////////////////////////////////// |
||
132 | /// <summary> |
||
16 | eva | 133 | /// Get the name of a structure member. |
13 | eva | 134 | /// </summary> |
135 | /// <typeparam name="T">the type of the structure to search</typeparam> |
||
136 | /// <param name="structure">the structure to search</param> |
||
137 | /// <param name="item">the value of the item to search</param> |
||
138 | /// <returns>the description or the empty string</returns> |
||
139 | public static string GetStructureMemberName<T>(T structure, object item) where T : struct |
||
140 | { |
||
16 | eva | 141 | var field = typeof (T).GetRuntimeFields().ToArray() |
13 | eva | 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)); |
||
144 | return field != null ? ((NameAttribute) field.Att).Name : string.Empty; |
||
145 | } |
||
146 | |||
16 | eva | 147 | /////////////////////////////////////////////////////////////////////////// |
148 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
149 | /////////////////////////////////////////////////////////////////////////// |
||
13 | eva | 150 | /// <summary> |
16 | eva | 151 | /// Get enumeration value from its description. |
152 | /// </summary> |
||
153 | /// <typeparam name="T">the enumeration type</typeparam> |
||
154 | /// <param name="description">the description of a member</param> |
||
155 | /// <returns>the value or the default of T if case no description found</returns> |
||
156 | public static T GetEnumValueFromDescription<T>(string description) |
||
157 | { |
||
158 | var field = typeof(T).GetRuntimeFields().ToArray() |
||
159 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
||
160 | typeof(DescriptionAttribute), false), ( |
||
161 | f, a) => new { Field = f, Att = a }).SingleOrDefault(a => ((DescriptionAttribute)a.Att) |
||
162 | .Description.Equals(description)); |
||
163 | return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
||
164 | } |
||
165 | |||
166 | /// <summary> |
||
13 | eva | 167 | /// A generic name attribute. |
168 | /// </summary> |
||
169 | public class NameAttribute : Attribute |
||
170 | { |
||
171 | protected readonly string name; |
||
172 | |||
173 | public NameAttribute(string name) |
||
174 | { |
||
175 | this.name = name; |
||
176 | } |
||
177 | |||
178 | public string Name => name; |
||
179 | } |
||
180 | |||
181 | /// <summary> |
||
182 | /// A generic description attribute. |
||
183 | /// </summary> |
||
184 | public class DescriptionAttribute : Attribute |
||
185 | { |
||
186 | protected readonly string description; |
||
187 | |||
188 | public DescriptionAttribute(string description) |
||
189 | { |
||
190 | this.description = description; |
||
191 | } |
||
192 | |||
193 | public string Description => description; |
||
194 | } |
||
195 | } |
||
196 | } |