wasSharp – Blame information for rev 33
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | zed | 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 | { |
||
7 | office | 14 | public static class Reflection |
2 | zed | 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> |
||
5 | eva | 23 | public static T GetAttributeFromEnumValue<T>(Enum value) where T : Attribute |
2 | zed | 24 | { |
27 | office | 25 | return (T)value.GetType() |
2 | zed | 26 | .GetRuntimeField(value.ToString()) |
10 | office | 27 | .GetCustomAttributes(typeof(T), false) |
2 | zed | 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> |
||
5 | eva | 39 | public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute |
2 | zed | 40 | { |
7 | office | 41 | return e.GetType().GetRuntimeFields().ToArray() |
3 | eva | 42 | .AsParallel() |
27 | office | 43 | .Select(o => GetAttributeFromEnumValue<T>((Enum)o.GetValue(Activator.CreateInstance<T>()))); |
2 | zed | 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> |
||
5 | eva | 53 | public static IEnumerable<string> GetEnumNames<T>() |
2 | zed | 54 | { |
55 | return |
||
10 | office | 56 | typeof(T).GetRuntimeFields().ToArray() |
2 | zed | 57 | .AsParallel() |
10 | office | 58 | .Select(o => o.GetCustomAttribute(typeof(NameAttribute), false)) |
2 | zed | 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> |
||
5 | eva | 71 | public static IEnumerable<T> GetEnumValues<T>() |
2 | zed | 72 | { |
27 | office | 73 | return Enum.GetValues(typeof(T)).Cast<object>().Select(value => (T)value); |
2 | zed | 74 | } |
75 | |||
76 | /////////////////////////////////////////////////////////////////////////// |
||
77 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
78 | /////////////////////////////////////////////////////////////////////////// |
||
79 | /// <summary> |
||
7 | office | 80 | /// Get the name from an enumeration value. |
2 | zed | 81 | /// </summary> |
82 | /// <param name="value">an enumeration value</param> |
||
83 | /// <returns>the description or the empty string</returns> |
||
5 | eva | 84 | public static string GetNameFromEnumValue(Enum value) |
2 | zed | 85 | { |
7 | office | 86 | var attribute = value.GetType() |
2 | zed | 87 | .GetRuntimeField(value.ToString()) |
10 | office | 88 | .GetCustomAttributes(typeof(NameAttribute), false) |
2 | zed | 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> |
||
5 | eva | 101 | public static string GetDescriptionFromEnumValue(Enum value) |
2 | zed | 102 | { |
7 | office | 103 | var attribute = value.GetType() |
2 | zed | 104 | .GetRuntimeField(value.ToString()) |
10 | office | 105 | .GetCustomAttributes(typeof(DescriptionAttribute), false) |
2 | zed | 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> |
||
21 | office | 118 | /// <param name="comparison">the string comparison to use</param> |
2 | zed | 119 | /// <returns>the value or the default of T if case no name attribute found</returns> |
24 | office | 120 | public static T GetEnumValueFromName<T>(string name, |
121 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
||
2 | zed | 122 | { |
10 | office | 123 | var field = typeof(T).GetRuntimeFields().ToArray() |
2 | zed | 124 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
10 | office | 125 | typeof(NameAttribute), false), ( |
27 | office | 126 | f, a) => new { Field = f, Att = a }) |
127 | .SingleOrDefault(a => string.Equals(((NameAttribute)a.Att) |
||
21 | office | 128 | .Name, name, comparison)); |
27 | office | 129 | return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
2 | zed | 130 | } |
131 | |||
132 | /////////////////////////////////////////////////////////////////////////// |
||
133 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
134 | /////////////////////////////////////////////////////////////////////////// |
||
135 | /// <summary> |
||
7 | office | 136 | /// Get the name of a structure member. |
2 | zed | 137 | /// </summary> |
138 | /// <typeparam name="T">the type of the structure to search</typeparam> |
||
139 | /// <param name="structure">the structure to search</param> |
||
140 | /// <param name="item">the value of the item to search</param> |
||
141 | /// <returns>the description or the empty string</returns> |
||
5 | eva | 142 | public static string GetStructureMemberName<T>(T structure, object item) where T : struct |
2 | zed | 143 | { |
10 | office | 144 | var field = typeof(T).GetRuntimeFields().ToArray() |
145 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false), |
||
27 | office | 146 | (f, a) => new { Field = f, Att = a }).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item)); |
147 | return field != null ? ((NameAttribute)field.Att).Name : string.Empty; |
||
2 | zed | 148 | } |
3 | eva | 149 | |
7 | office | 150 | /////////////////////////////////////////////////////////////////////////// |
151 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
152 | /////////////////////////////////////////////////////////////////////////// |
||
3 | eva | 153 | /// <summary> |
7 | office | 154 | /// Get field or property from a class by supplying a path. |
155 | /// </summary> |
||
156 | /// <typeparam name="T">the type of the object</typeparam> |
||
157 | /// <param name="o">the object</param> |
||
158 | /// <param name="path">the fully qualified path to the field of property</param> |
||
159 | /// <returns> |
||
160 | /// the last object in the fully qualified path or null in case the field or property could not be found |
||
161 | /// </returns> |
||
162 | public static object GetFP<T>(this T o, string path) |
||
163 | { |
||
164 | if (string.IsNullOrEmpty(path)) return null; |
||
165 | if (o == null) return null; |
||
166 | |||
167 | var memberType = o.GetType(); |
||
168 | var components = path.Split('.'); |
||
169 | |||
170 | var f = memberType.GetRuntimeField(components[0]); |
||
171 | var p = memberType.GetRuntimeProperty(components[0]); |
||
172 | |||
173 | if (f != null) |
||
174 | return components.Length > 1 |
||
175 | ? GetFP(f.GetValue(o), |
||
176 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
177 | : memberType.GetRuntimeField(path).GetValue(o); |
||
178 | |||
179 | if (p != null) |
||
180 | return components.Length > 1 |
||
181 | ? GetFP(p.GetValue(o), |
||
182 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
183 | : memberType.GetRuntimeProperty(path).GetValue(o); |
||
184 | |||
185 | return null; |
||
186 | } |
||
187 | |||
188 | /////////////////////////////////////////////////////////////////////////// |
||
189 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
190 | /////////////////////////////////////////////////////////////////////////// |
||
191 | /// <summary> |
||
192 | /// Get field or property info from a class by supplying a path. |
||
193 | /// </summary> |
||
194 | /// <typeparam name="T">the type of the object</typeparam> |
||
195 | /// <param name="o">the object</param> |
||
196 | /// <param name="path">the fully qualified path to the field of property</param> |
||
197 | /// <returns> |
||
198 | /// the field or property info of the last object in the path or null if the object cannot be found |
||
199 | /// </returns> |
||
200 | public static object GetFPInfo<T>(this T o, string path) |
||
201 | { |
||
202 | if (string.IsNullOrEmpty(path)) return null; |
||
203 | if (o == null) return null; |
||
204 | |||
205 | var memberType = o.GetType(); |
||
206 | var components = path.Split('.'); |
||
207 | |||
208 | var f = memberType.GetRuntimeField(components[0]); |
||
209 | var p = memberType.GetRuntimeProperty(components[0]); |
||
210 | |||
211 | if (f != null) |
||
212 | return components.Length > 1 |
||
213 | ? GetFPInfo(f.GetValue(o), |
||
214 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
215 | : memberType.GetRuntimeField(path); |
||
216 | |||
217 | if (p != null) |
||
218 | return components.Length > 1 |
||
219 | ? GetFPInfo(p.GetValue(o), |
||
220 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
221 | : memberType.GetRuntimeProperty(path); |
||
222 | |||
223 | return null; |
||
224 | } |
||
225 | |||
226 | /////////////////////////////////////////////////////////////////////////// |
||
227 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
228 | /////////////////////////////////////////////////////////////////////////// |
||
19 | office | 229 | /// <summary>Return all the run-time properties for an object.</summary> |
230 | /// <param name="o">the object whose properties to return</param> |
||
231 | /// <returns>the property information for all the properties of the object</returns> |
||
232 | public static IEnumerable<PropertyInfo> GetPropertiesInfo<T>(this T o) |
||
233 | { |
||
234 | foreach (var p in o.GetType().GetRuntimeProperties()) |
||
235 | yield return p; |
||
236 | } |
||
237 | |||
238 | /////////////////////////////////////////////////////////////////////////// |
||
239 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
240 | /////////////////////////////////////////////////////////////////////////// |
||
7 | office | 241 | /// <summary> |
242 | /// Enumerate all the base types recursively starting from a type. |
||
243 | /// </summary> |
||
244 | /// <param name="type">the type</param> |
||
245 | /// <returns>an enumeration of all base types</returns> |
||
246 | public static IEnumerable<Type> GetBaseTypes(this Type type) |
||
247 | { |
||
248 | var baseType = type.GetTypeInfo().BaseType; |
||
10 | office | 249 | if (baseType == null) |
7 | office | 250 | yield break; |
251 | yield return baseType; |
||
252 | foreach (var t in GetBaseTypes(baseType)) |
||
253 | { |
||
254 | yield return t; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /// <summary> |
||
3 | eva | 259 | /// A generic name attribute. |
260 | /// </summary> |
||
261 | public class NameAttribute : Attribute |
||
262 | { |
||
263 | protected readonly string name; |
||
264 | |||
265 | public NameAttribute(string name) |
||
266 | { |
||
267 | this.name = name; |
||
268 | } |
||
269 | |||
270 | public string Name => name; |
||
271 | } |
||
272 | |||
273 | /// <summary> |
||
274 | /// A generic description attribute. |
||
275 | /// </summary> |
||
276 | public class DescriptionAttribute : Attribute |
||
277 | { |
||
278 | protected readonly string description; |
||
279 | |||
280 | public DescriptionAttribute(string description) |
||
281 | { |
||
282 | this.description = description; |
||
283 | } |
||
284 | |||
285 | public string Description => description; |
||
286 | } |
||
2 | zed | 287 | } |
27 | office | 288 | } |