wasSharp – Blame information for rev 7

Subversion Repositories:
Rev:
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;
7 office 11 using wasSharp;
2 zed 12  
13 namespace wasSharp
14 {
7 office 15 public static class Reflection
2 zed 16 {
17 ///////////////////////////////////////////////////////////////////////////
18 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
19 ///////////////////////////////////////////////////////////////////////////
20 /// <summary>
21 /// Retrieves an attribute of type T from an enumeration.
22 /// </summary>
23 /// <returns>an attribute of type T</returns>
5 eva 24 public static T GetAttributeFromEnumValue<T>(Enum value) where T : Attribute
2 zed 25 {
3 eva 26 return (T) value.GetType()
2 zed 27 .GetRuntimeField(value.ToString())
3 eva 28 .GetCustomAttributes(typeof (T), false)
2 zed 29 .SingleOrDefault();
30 }
31  
32 ///////////////////////////////////////////////////////////////////////////
33 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
34 ///////////////////////////////////////////////////////////////////////////
35 /// <summary>
36 /// Returns all the attributes of type T of an enumeration.
37 /// </summary>
38 /// <typeparam name="T">the attribute to retrieve</typeparam>
39 /// <returns>a list of attributes</returns>
5 eva 40 public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute
2 zed 41 {
7 office 42 return e.GetType().GetRuntimeFields().ToArray()
3 eva 43 .AsParallel()
5 eva 44 .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>())));
2 zed 45 }
46  
47 ///////////////////////////////////////////////////////////////////////////
48 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
49 ///////////////////////////////////////////////////////////////////////////
50 /// <summary>
51 /// Returns all the field names of an enumeration.
52 /// </summary>
53 /// <returns>the field names</returns>
5 eva 54 public static IEnumerable<string> GetEnumNames<T>()
2 zed 55 {
56 return
7 office 57 typeof (T).GetRuntimeFields().ToArray()
2 zed 58 .AsParallel()
59 .Select(o => o.GetCustomAttribute(typeof (NameAttribute), false))
60 .Select(o => (o as NameAttribute)?.Name)
61 .Where(o => !string.IsNullOrEmpty(o))
62 .Select(o => o);
63 }
64  
65 ///////////////////////////////////////////////////////////////////////////
66 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
67 ///////////////////////////////////////////////////////////////////////////
68 /// <summary>
69 /// Returns all the values of an enumeration.
70 /// </summary>
71 /// <returns>the values of the enumeration</returns>
5 eva 72 public static IEnumerable<T> GetEnumValues<T>()
2 zed 73 {
74 return Enum.GetValues(typeof (T)).Cast<object>().Select(value => (T) value);
75 }
76  
77 ///////////////////////////////////////////////////////////////////////////
78 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
79 ///////////////////////////////////////////////////////////////////////////
80 /// <summary>
7 office 81 /// Get the name from an enumeration value.
2 zed 82 /// </summary>
83 /// <param name="value">an enumeration value</param>
84 /// <returns>the description or the empty string</returns>
5 eva 85 public static string GetNameFromEnumValue(Enum value)
2 zed 86 {
7 office 87 var attribute = value.GetType()
2 zed 88 .GetRuntimeField(value.ToString())
3 eva 89 .GetCustomAttributes(typeof (NameAttribute), false)
2 zed 90 .SingleOrDefault() as NameAttribute;
91 return attribute?.Name;
92 }
93  
94 ///////////////////////////////////////////////////////////////////////////
95 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
96 ///////////////////////////////////////////////////////////////////////////
97 /// <summary>
98 /// Get the description from an enumeration value.
99 /// </summary>
100 /// <param name="value">an enumeration value</param>
101 /// <returns>the description or the empty string</returns>
5 eva 102 public static string GetDescriptionFromEnumValue(Enum value)
2 zed 103 {
7 office 104 var attribute = value.GetType()
2 zed 105 .GetRuntimeField(value.ToString())
3 eva 106 .GetCustomAttributes(typeof (DescriptionAttribute), false)
2 zed 107 .SingleOrDefault() as DescriptionAttribute;
108 return attribute?.Description;
109 }
110  
111 ///////////////////////////////////////////////////////////////////////////
112 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
113 ///////////////////////////////////////////////////////////////////////////
114 /// <summary>
115 /// Get enumeration value from its name attribute.
116 /// </summary>
117 /// <typeparam name="T">the enumeration type</typeparam>
118 /// <param name="name">the description of a member</param>
119 /// <returns>the value or the default of T if case no name attribute found</returns>
5 eva 120 public static T GetEnumValueFromName<T>(string name)
2 zed 121 {
7 office 122 var field = typeof (T).GetRuntimeFields().ToArray()
2 zed 123 .AsParallel().SelectMany(f => f.GetCustomAttributes(
3 eva 124 typeof (NameAttribute), false), (
7 office 125 f, a) => new {Field = f, Att = a}).SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att)
126 .Name, name, StringComparison.Ordinal));
3 eva 127 return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
2 zed 128 }
129  
130 ///////////////////////////////////////////////////////////////////////////
131 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
132 ///////////////////////////////////////////////////////////////////////////
133 /// <summary>
7 office 134 /// Get the name of a structure member.
2 zed 135 /// </summary>
136 /// <typeparam name="T">the type of the structure to search</typeparam>
137 /// <param name="structure">the structure to search</param>
138 /// <param name="item">the value of the item to search</param>
139 /// <returns>the description or the empty string</returns>
5 eva 140 public static string GetStructureMemberName<T>(T structure, object item) where T : struct
2 zed 141 {
7 office 142 var field = typeof (T).GetRuntimeFields().ToArray()
3 eva 143 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false),
144 (f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item));
145 return field != null ? ((NameAttribute) field.Att).Name : string.Empty;
2 zed 146 }
3 eva 147  
7 office 148 ///////////////////////////////////////////////////////////////////////////
149 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
150 ///////////////////////////////////////////////////////////////////////////
3 eva 151 /// <summary>
7 office 152 /// Get field or property from a class by supplying a path.
153 /// </summary>
154 /// <typeparam name="T">the type of the object</typeparam>
155 /// <param name="o">the object</param>
156 /// <param name="path">the fully qualified path to the field of property</param>
157 /// <returns>
158 /// the last object in the fully qualified path or null in case the field or property could not be found
159 /// </returns>
160 public static object GetFP<T>(this T o, string path)
161 {
162 if (string.IsNullOrEmpty(path)) return null;
163 if (o == null) return null;
164  
165 var memberType = o.GetType();
166 var components = path.Split('.');
167  
168 var f = memberType.GetRuntimeField(components[0]);
169 var p = memberType.GetRuntimeProperty(components[0]);
170  
171 if (f != null)
172 return components.Length > 1
173 ? GetFP(f.GetValue(o),
174 components.Skip(1).Aggregate((a, i) => a + @"." + i))
175 : memberType.GetRuntimeField(path).GetValue(o);
176  
177 if (p != null)
178 return components.Length > 1
179 ? GetFP(p.GetValue(o),
180 components.Skip(1).Aggregate((a, i) => a + @"." + i))
181 : memberType.GetRuntimeProperty(path).GetValue(o);
182  
183 return null;
184 }
185  
186 ///////////////////////////////////////////////////////////////////////////
187 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
188 ///////////////////////////////////////////////////////////////////////////
189 /// <summary>
190 /// Get field or property info from a class by supplying a path.
191 /// </summary>
192 /// <typeparam name="T">the type of the object</typeparam>
193 /// <param name="o">the object</param>
194 /// <param name="path">the fully qualified path to the field of property</param>
195 /// <returns>
196 /// the field or property info of the last object in the path or null if the object cannot be found
197 /// </returns>
198 public static object GetFPInfo<T>(this T o, string path)
199 {
200 if (string.IsNullOrEmpty(path)) return null;
201 if (o == null) return null;
202  
203 var memberType = o.GetType();
204 var components = path.Split('.');
205  
206 var f = memberType.GetRuntimeField(components[0]);
207 var p = memberType.GetRuntimeProperty(components[0]);
208  
209 if (f != null)
210 return components.Length > 1
211 ? GetFPInfo(f.GetValue(o),
212 components.Skip(1).Aggregate((a, i) => a + @"." + i))
213 : memberType.GetRuntimeField(path);
214  
215 if (p != null)
216 return components.Length > 1
217 ? GetFPInfo(p.GetValue(o),
218 components.Skip(1).Aggregate((a, i) => a + @"." + i))
219 : memberType.GetRuntimeProperty(path);
220  
221 return null;
222 }
223  
224 ///////////////////////////////////////////////////////////////////////////
225 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
226 ///////////////////////////////////////////////////////////////////////////
227 /// <summary>
228 /// Enumerate all the base types recursively starting from a type.
229 /// </summary>
230 /// <param name="type">the type</param>
231 /// <returns>an enumeration of all base types</returns>
232 public static IEnumerable<Type> GetBaseTypes(this Type type)
233 {
234 var baseType = type.GetTypeInfo().BaseType;
235 if(baseType == null)
236 yield break;
237 yield return baseType;
238 foreach (var t in GetBaseTypes(baseType))
239 {
240 yield return t;
241 }
242 }
243  
244 /// <summary>
3 eva 245 /// A generic name attribute.
246 /// </summary>
247 public class NameAttribute : Attribute
248 {
249 protected readonly string name;
250  
251 public NameAttribute(string name)
252 {
253 this.name = name;
254 }
255  
256 public string Name => name;
257 }
258  
259 /// <summary>
260 /// A generic description attribute.
261 /// </summary>
262 public class DescriptionAttribute : Attribute
263 {
264 protected readonly string description;
265  
266 public DescriptionAttribute(string description)
267 {
268 this.description = description;
269 }
270  
271 public string Description => description;
272 }
2 zed 273 }
3 eva 274 }