wasSharp – Diff between revs 2 and 3

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 2 Rev 3
Line 6... Line 6...
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; -  
11 using System.Text; -  
Line 12... Line 10...
12 using System.Threading.Tasks; 10 using System.Reflection;
13   11  
14 namespace wasSharp 12 namespace wasSharp
15 { 13 {
16 public class Reflection -  
17 { -  
18   -  
19 /// <summary> -  
20 /// A generic name attribute. -  
21 /// </summary> -  
22 public class NameAttribute : Attribute -  
23 { -  
24 protected readonly string name; -  
25   -  
26 public NameAttribute(string name) -  
27 { -  
28 this.name = name; -  
29 } -  
30   -  
31 public string Name => name; -  
32 } -  
33   -  
34 /// <summary> -  
35 /// A generic description attribute. -  
36 /// </summary> -  
37 public class DescriptionAttribute : Attribute -  
38 { -  
39 protected readonly string description; -  
40   -  
41 public DescriptionAttribute(string description) -  
42 { -  
43 this.description = description; -  
44 } -  
45   -  
46 public string Description => description; -  
47 } 14 public class Reflection
48   15 {
49 /////////////////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////////
50 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
51 /////////////////////////////////////////////////////////////////////////// 18 ///////////////////////////////////////////////////////////////////////////
52 /// <summary> 19 /// <summary>
53 /// Retrieves an attribute of type T from an enumeration. 20 /// Retrieves an attribute of type T from an enumeration.
54 /// </summary> 21 /// </summary>
55 /// <returns>an attribute of type T</returns> 22 /// <returns>an attribute of type T</returns>
56 public static T wasGetAttributeFromEnumValue<T>(Enum value) where T : Attribute 23 public static T wasGetAttributeFromEnumValue<T>(Enum value) where T : Attribute
57 { 24 {
58 return (T)value.GetType() 25 return (T) value.GetType()
59 .GetRuntimeField(value.ToString()) 26 .GetRuntimeField(value.ToString())
60 .GetCustomAttributes(typeof(T), false) 27 .GetCustomAttributes(typeof (T), false)
Line 61... Line 28...
61 .SingleOrDefault(); 28 .SingleOrDefault();
62 } 29 }
Line 70... Line 37...
70 /// <typeparam name="T">the attribute to retrieve</typeparam> 37 /// <typeparam name="T">the attribute to retrieve</typeparam>
71 /// <returns>a list of attributes</returns> 38 /// <returns>a list of attributes</returns>
72 public static IEnumerable<T> wasGetEnumAttributes<T>(Enum e) where T : Attribute 39 public static IEnumerable<T> wasGetEnumAttributes<T>(Enum e) where T : Attribute
73 { 40 {
74 return e.GetType().GetRuntimeFields() 41 return e.GetType().GetRuntimeFields()
-   42 .AsParallel()
75 .AsParallel().Select(o => wasGetAttributeFromEnumValue<T>((Enum)o.GetValue(Activator.CreateInstance<T>()))); 43 .Select(o => wasGetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>())));
76 } 44 }
Line 77... Line 45...
77   45  
78 /////////////////////////////////////////////////////////////////////////// 46 ///////////////////////////////////////////////////////////////////////////
79 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 47 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
Line 115... Line 83...
115 /// <returns>the description or the empty string</returns> 83 /// <returns>the description or the empty string</returns>
116 public static string wasGetNameFromEnumValue(Enum value) 84 public static string wasGetNameFromEnumValue(Enum value)
117 { 85 {
118 NameAttribute attribute = value.GetType() 86 NameAttribute attribute = value.GetType()
119 .GetRuntimeField(value.ToString()) 87 .GetRuntimeField(value.ToString())
120 .GetCustomAttributes(typeof(NameAttribute), false) 88 .GetCustomAttributes(typeof (NameAttribute), false)
121 .SingleOrDefault() as NameAttribute; 89 .SingleOrDefault() as NameAttribute;
122 return attribute?.Name; 90 return attribute?.Name;
123 } 91 }
Line 124... Line 92...
124   92  
Line 132... Line 100...
132 /// <returns>the description or the empty string</returns> 100 /// <returns>the description or the empty string</returns>
133 public static string wasGetDescriptionFromEnumValue(Enum value) 101 public static string wasGetDescriptionFromEnumValue(Enum value)
134 { 102 {
135 DescriptionAttribute attribute = value.GetType() 103 DescriptionAttribute attribute = value.GetType()
136 .GetRuntimeField(value.ToString()) 104 .GetRuntimeField(value.ToString())
137 .GetCustomAttributes(typeof(DescriptionAttribute), false) 105 .GetCustomAttributes(typeof (DescriptionAttribute), false)
138 .SingleOrDefault() as DescriptionAttribute; 106 .SingleOrDefault() as DescriptionAttribute;
139 return attribute?.Description; 107 return attribute?.Description;
140 } 108 }
Line 141... Line 109...
141   109  
Line 148... Line 116...
148 /// <typeparam name="T">the enumeration type</typeparam> 116 /// <typeparam name="T">the enumeration type</typeparam>
149 /// <param name="name">the description of a member</param> 117 /// <param name="name">the description of a member</param>
150 /// <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>
151 public static T wasGetEnumValueFromName<T>(string name) 119 public static T wasGetEnumValueFromName<T>(string name)
152 { 120 {
153 var field = typeof(T).GetRuntimeFields() 121 var field = typeof (T).GetRuntimeFields()
154 .AsParallel().SelectMany(f => f.GetCustomAttributes( 122 .AsParallel().SelectMany(f => f.GetCustomAttributes(
155 typeof(NameAttribute), false), ( 123 typeof (NameAttribute), false), (
156 f, a) => new { Field = f, Att = a }).SingleOrDefault(a => ((NameAttribute)a.Att) 124 f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att)
157 .Name.Equals(name)); 125 .Name.Equals(name));
158 return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); 126 return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
159 } 127 }
Line 160... Line 128...
160   128  
161 /////////////////////////////////////////////////////////////////////////// 129 ///////////////////////////////////////////////////////////////////////////
162 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 130 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
Line 168... Line 136...
168 /// <param name="structure">the structure to search</param> 136 /// <param name="structure">the structure to search</param>
169 /// <param name="item">the value of the item to search</param> 137 /// <param name="item">the value of the item to search</param>
170 /// <returns>the description or the empty string</returns> 138 /// <returns>the description or the empty string</returns>
171 public static string wasGetStructureMemberName<T>(T structure, object item) where T : struct 139 public static string wasGetStructureMemberName<T>(T structure, object item) where T : struct
172 { 140 {
173 var field = typeof(T).GetRuntimeFields() 141 var field = typeof (T).GetRuntimeFields()
174 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false), 142 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false),
175 (f, a) => new { Field = f, Att = a }).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item)); 143 (f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item));
176 return field != null ? ((NameAttribute)field.Att).Name : string.Empty; 144 return field != null ? ((NameAttribute) field.Att).Name : string.Empty;
-   145 }
-   146  
-   147 /// <summary>
-   148 /// A generic name attribute.
-   149 /// </summary>
-   150 public class NameAttribute : Attribute
-   151 {
-   152 protected readonly string name;
-   153  
-   154 public NameAttribute(string name)
-   155 {
-   156 this.name = name;
-   157 }
-   158  
-   159 public string Name => name;
-   160 }
-   161  
-   162 /// <summary>
-   163 /// A generic description attribute.
-   164 /// </summary>
-   165 public class DescriptionAttribute : Attribute
-   166 {
-   167 protected readonly string description;
-   168  
-   169 public DescriptionAttribute(string description)
-   170 {
-   171 this.description = description;
-   172 }
-   173  
-   174 public string Description => description;
177 } 175 }
178 } 176 }
179 } 177 }
180   178