wasSharp – Diff between revs 7 and 10

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