wasSharp – Diff between revs 19 and 21

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 19 Rev 21
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.Linq; 9 using System.Linq;
10 using System.Reflection; 10 using System.Reflection;
11   11  
12 namespace wasSharp 12 namespace wasSharp
13 { 13 {
14 public static class Reflection 14 public static class Reflection
15 { 15 {
16 /////////////////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
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 GetAttributeFromEnumValue<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();
29 } 29 }
30   30  
31 /////////////////////////////////////////////////////////////////////////// 31 ///////////////////////////////////////////////////////////////////////////
32 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 32 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
33 /////////////////////////////////////////////////////////////////////////// 33 ///////////////////////////////////////////////////////////////////////////
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> GetEnumAttributes<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().ToArray() 41 return e.GetType().GetRuntimeFields().ToArray()
42 .AsParallel() 42 .AsParallel()
43 .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>()))); 43 .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>())));
44 } 44 }
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> GetEnumNames<T>() 53 public static IEnumerable<string> GetEnumNames<T>()
54 { 54 {
55 return 55 return
56 typeof(T).GetRuntimeFields().ToArray() 56 typeof(T).GetRuntimeFields().ToArray()
57 .AsParallel() 57 .AsParallel()
58 .Select(o => o.GetCustomAttribute(typeof(NameAttribute), false)) 58 .Select(o => o.GetCustomAttribute(typeof(NameAttribute), false))
59 .Select(o => (o as NameAttribute)?.Name) 59 .Select(o => (o as NameAttribute)?.Name)
60 .Where(o => !string.IsNullOrEmpty(o)) 60 .Where(o => !string.IsNullOrEmpty(o))
61 .Select(o => o); 61 .Select(o => o);
62 } 62 }
63   63  
64 /////////////////////////////////////////////////////////////////////////// 64 ///////////////////////////////////////////////////////////////////////////
65 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 65 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
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> GetEnumValues<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 }
75   75  
76 /////////////////////////////////////////////////////////////////////////// 76 ///////////////////////////////////////////////////////////////////////////
77 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 77 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
78 /////////////////////////////////////////////////////////////////////////// 78 ///////////////////////////////////////////////////////////////////////////
79 /// <summary> 79 /// <summary>
80 /// Get the name from an enumeration value. 80 /// Get the name 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 GetNameFromEnumValue(Enum value) 84 public static string GetNameFromEnumValue(Enum value)
85 { 85 {
86 var attribute = value.GetType() 86 var 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;
90 return attribute?.Name; 90 return attribute?.Name;
91 } 91 }
92   92  
93 /////////////////////////////////////////////////////////////////////////// 93 ///////////////////////////////////////////////////////////////////////////
94 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 94 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
95 /////////////////////////////////////////////////////////////////////////// 95 ///////////////////////////////////////////////////////////////////////////
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 GetDescriptionFromEnumValue(Enum value) 101 public static string GetDescriptionFromEnumValue(Enum value)
102 { 102 {
103 var attribute = value.GetType() 103 var 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;
107 return attribute?.Description; 107 return attribute?.Description;
108 } 108 }
109   109  
110 /////////////////////////////////////////////////////////////////////////// 110 ///////////////////////////////////////////////////////////////////////////
111 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 111 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
112 /////////////////////////////////////////////////////////////////////////// 112 ///////////////////////////////////////////////////////////////////////////
113 /// <summary> 113 /// <summary>
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 /// <param name="comparison">the string comparison to use</param>
118 /// <returns>the value or the default of T if case no name attribute found</returns> 119 /// <returns>the value or the default of T if case no name attribute found</returns>
119 public static T GetEnumValueFromName<T>(string name) 120 public static T GetEnumValueFromName<T>(string name, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
120 { 121 {
121 var field = typeof(T).GetRuntimeFields().ToArray() 122 var field = typeof(T).GetRuntimeFields().ToArray()
122 .AsParallel().SelectMany(f => f.GetCustomAttributes( 123 .AsParallel().SelectMany(f => f.GetCustomAttributes(
123 typeof(NameAttribute), false), ( 124 typeof(NameAttribute), false), (
124 f, a) => new {Field = f, Att = a}) 125 f, a) => new {Field = f, Att = a})
125 .SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att) 126 .SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att)
126 .Name, name, StringComparison.Ordinal)); 127 .Name, name, comparison));
127 return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); 128 return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
128 } 129 }
129   130  
130 /////////////////////////////////////////////////////////////////////////// 131 ///////////////////////////////////////////////////////////////////////////
131 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 132 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
132 /////////////////////////////////////////////////////////////////////////// 133 ///////////////////////////////////////////////////////////////////////////
133 /// <summary> 134 /// <summary>
134 /// Get the name of a structure member. 135 /// Get the name of a structure member.
135 /// </summary> 136 /// </summary>
136 /// <typeparam name="T">the type of the structure to search</typeparam> 137 /// <typeparam name="T">the type of the structure to search</typeparam>
137 /// <param name="structure">the structure to search</param> 138 /// <param name="structure">the structure to search</param>
138 /// <param name="item">the value of the item to search</param> 139 /// <param name="item">the value of the item to search</param>
139 /// <returns>the description or the empty string</returns> 140 /// <returns>the description or the empty string</returns>
140 public static string GetStructureMemberName<T>(T structure, object item) where T : struct 141 public static string GetStructureMemberName<T>(T structure, object item) where T : struct
141 { 142 {
142 var field = typeof(T).GetRuntimeFields().ToArray() 143 var field = typeof(T).GetRuntimeFields().ToArray()
143 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false), 144 .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 (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; 146 return field != null ? ((NameAttribute) field.Att).Name : string.Empty;
146 } 147 }
147   148  
148 /////////////////////////////////////////////////////////////////////////// 149 ///////////////////////////////////////////////////////////////////////////
149 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 150 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
150 /////////////////////////////////////////////////////////////////////////// 151 ///////////////////////////////////////////////////////////////////////////
151 /// <summary> 152 /// <summary>
152 /// Get field or property from a class by supplying a path. 153 /// Get field or property from a class by supplying a path.
153 /// </summary> 154 /// </summary>
154 /// <typeparam name="T">the type of the object</typeparam> 155 /// <typeparam name="T">the type of the object</typeparam>
155 /// <param name="o">the object</param> 156 /// <param name="o">the object</param>
156 /// <param name="path">the fully qualified path to the field of property</param> 157 /// <param name="path">the fully qualified path to the field of property</param>
157 /// <returns> 158 /// <returns>
158 /// the last object in the fully qualified path or null in case the field or property could not be found 159 /// the last object in the fully qualified path or null in case the field or property could not be found
159 /// </returns> 160 /// </returns>
160 public static object GetFP<T>(this T o, string path) 161 public static object GetFP<T>(this T o, string path)
161 { 162 {
162 if (string.IsNullOrEmpty(path)) return null; 163 if (string.IsNullOrEmpty(path)) return null;
163 if (o == null) return null; 164 if (o == null) return null;
164   165  
165 var memberType = o.GetType(); 166 var memberType = o.GetType();
166 var components = path.Split('.'); 167 var components = path.Split('.');
167   168  
168 var f = memberType.GetRuntimeField(components[0]); 169 var f = memberType.GetRuntimeField(components[0]);
169 var p = memberType.GetRuntimeProperty(components[0]); 170 var p = memberType.GetRuntimeProperty(components[0]);
170   171  
171 if (f != null) 172 if (f != null)
172 return components.Length > 1 173 return components.Length > 1
173 ? GetFP(f.GetValue(o), 174 ? GetFP(f.GetValue(o),
174 components.Skip(1).Aggregate((a, i) => a + @"." + i)) 175 components.Skip(1).Aggregate((a, i) => a + @"." + i))
175 : memberType.GetRuntimeField(path).GetValue(o); 176 : memberType.GetRuntimeField(path).GetValue(o);
176   177  
177 if (p != null) 178 if (p != null)
178 return components.Length > 1 179 return components.Length > 1
179 ? GetFP(p.GetValue(o), 180 ? GetFP(p.GetValue(o),
180 components.Skip(1).Aggregate((a, i) => a + @"." + i)) 181 components.Skip(1).Aggregate((a, i) => a + @"." + i))
181 : memberType.GetRuntimeProperty(path).GetValue(o); 182 : memberType.GetRuntimeProperty(path).GetValue(o);
182   183  
183 return null; 184 return null;
184 } 185 }
185   186  
186 /////////////////////////////////////////////////////////////////////////// 187 ///////////////////////////////////////////////////////////////////////////
187 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 188 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
188 /////////////////////////////////////////////////////////////////////////// 189 ///////////////////////////////////////////////////////////////////////////
189 /// <summary> 190 /// <summary>
190 /// Get field or property info from a class by supplying a path. 191 /// Get field or property info from a class by supplying a path.
191 /// </summary> 192 /// </summary>
192 /// <typeparam name="T">the type of the object</typeparam> 193 /// <typeparam name="T">the type of the object</typeparam>
193 /// <param name="o">the object</param> 194 /// <param name="o">the object</param>
194 /// <param name="path">the fully qualified path to the field of property</param> 195 /// <param name="path">the fully qualified path to the field of property</param>
195 /// <returns> 196 /// <returns>
196 /// the field or property info of the last object in the path or null if the object cannot be found 197 /// the field or property info of the last object in the path or null if the object cannot be found
197 /// </returns> 198 /// </returns>
198 public static object GetFPInfo<T>(this T o, string path) 199 public static object GetFPInfo<T>(this T o, string path)
199 { 200 {
200 if (string.IsNullOrEmpty(path)) return null; 201 if (string.IsNullOrEmpty(path)) return null;
201 if (o == null) return null; 202 if (o == null) return null;
202   203  
203 var memberType = o.GetType(); 204 var memberType = o.GetType();
204 var components = path.Split('.'); 205 var components = path.Split('.');
205   206  
206 var f = memberType.GetRuntimeField(components[0]); 207 var f = memberType.GetRuntimeField(components[0]);
207 var p = memberType.GetRuntimeProperty(components[0]); 208 var p = memberType.GetRuntimeProperty(components[0]);
208   209  
209 if (f != null) 210 if (f != null)
210 return components.Length > 1 211 return components.Length > 1
211 ? GetFPInfo(f.GetValue(o), 212 ? GetFPInfo(f.GetValue(o),
212 components.Skip(1).Aggregate((a, i) => a + @"." + i)) 213 components.Skip(1).Aggregate((a, i) => a + @"." + i))
213 : memberType.GetRuntimeField(path); 214 : memberType.GetRuntimeField(path);
214   215  
215 if (p != null) 216 if (p != null)
216 return components.Length > 1 217 return components.Length > 1
217 ? GetFPInfo(p.GetValue(o), 218 ? GetFPInfo(p.GetValue(o),
218 components.Skip(1).Aggregate((a, i) => a + @"." + i)) 219 components.Skip(1).Aggregate((a, i) => a + @"." + i))
219 : memberType.GetRuntimeProperty(path); 220 : memberType.GetRuntimeProperty(path);
220   221  
221 return null; 222 return null;
222 } 223 }
223   224  
224 /////////////////////////////////////////////////////////////////////////// 225 ///////////////////////////////////////////////////////////////////////////
225 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 226 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
226 /////////////////////////////////////////////////////////////////////////// 227 ///////////////////////////////////////////////////////////////////////////
227 /// <summary>Return all the run-time properties for an object.</summary> 228 /// <summary>Return all the run-time properties for an object.</summary>
228 /// <param name="o">the object whose properties to return</param> 229 /// <param name="o">the object whose properties to return</param>
229 /// <returns>the property information for all the properties of the object</returns> 230 /// <returns>the property information for all the properties of the object</returns>
230 public static IEnumerable<PropertyInfo> GetPropertiesInfo<T>(this T o) 231 public static IEnumerable<PropertyInfo> GetPropertiesInfo<T>(this T o)
231 { 232 {
232 foreach (var p in o.GetType().GetRuntimeProperties()) 233 foreach (var p in o.GetType().GetRuntimeProperties())
233 yield return p; 234 yield return p;
234 } 235 }
235   236  
236 /////////////////////////////////////////////////////////////////////////// 237 ///////////////////////////////////////////////////////////////////////////
237 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 238 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
238 /////////////////////////////////////////////////////////////////////////// 239 ///////////////////////////////////////////////////////////////////////////
239 /// <summary> 240 /// <summary>
240 /// Enumerate all the base types recursively starting from a type. 241 /// Enumerate all the base types recursively starting from a type.
241 /// </summary> 242 /// </summary>
242 /// <param name="type">the type</param> 243 /// <param name="type">the type</param>
243 /// <returns>an enumeration of all base types</returns> 244 /// <returns>an enumeration of all base types</returns>
244 public static IEnumerable<Type> GetBaseTypes(this Type type) 245 public static IEnumerable<Type> GetBaseTypes(this Type type)
245 { 246 {
246 var baseType = type.GetTypeInfo().BaseType; 247 var baseType = type.GetTypeInfo().BaseType;
247 if (baseType == null) 248 if (baseType == null)
248 yield break; 249 yield break;
249 yield return baseType; 250 yield return baseType;
250 foreach (var t in GetBaseTypes(baseType)) 251 foreach (var t in GetBaseTypes(baseType))
251 { 252 {
252 yield return t; 253 yield return t;
253 } 254 }
254 } 255 }
255   256  
256 /// <summary> 257 /// <summary>
257 /// A generic name attribute. 258 /// A generic name attribute.
258 /// </summary> 259 /// </summary>
259 public class NameAttribute : Attribute 260 public class NameAttribute : Attribute
260 { 261 {
261 protected readonly string name; 262 protected readonly string name;
262   263  
263 public NameAttribute(string name) 264 public NameAttribute(string name)
264 { 265 {
265 this.name = name; 266 this.name = name;
266 } 267 }
267   268  
268 public string Name => name; 269 public string Name => name;
269 } 270 }
270   271  
271 /// <summary> 272 /// <summary>
272 /// A generic description attribute. 273 /// A generic description attribute.
273 /// </summary> 274 /// </summary>
274 public class DescriptionAttribute : Attribute 275 public class DescriptionAttribute : Attribute
275 { 276 {
276 protected readonly string description; 277 protected readonly string description;
277   278  
278 public DescriptionAttribute(string description) 279 public DescriptionAttribute(string description)
279 { 280 {
280 this.description = description; 281 this.description = description;
281 } 282 }
282   283  
283 public string Description => description; 284 public string Description => description;
284 } 285 }
285 } 286 }
286 } 287 }
287   288  
288
Generated by GNU Enscript 1.6.5.90.
289
Generated by GNU Enscript 1.6.5.90.
289   290  
290   291  
291   292