wasSharp – Blame information for rev 2

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;
11 using System.Text;
12 using System.Threading.Tasks;
13  
14 namespace wasSharp
15 {
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 }
48  
49 ///////////////////////////////////////////////////////////////////////////
50 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
51 ///////////////////////////////////////////////////////////////////////////
52 /// <summary>
53 /// Retrieves an attribute of type T from an enumeration.
54 /// </summary>
55 /// <returns>an attribute of type T</returns>
56 public static T wasGetAttributeFromEnumValue<T>(Enum value) where T : Attribute
57 {
58 return (T)value.GetType()
59 .GetRuntimeField(value.ToString())
60 .GetCustomAttributes(typeof(T), false)
61 .SingleOrDefault();
62 }
63  
64 ///////////////////////////////////////////////////////////////////////////
65 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
66 ///////////////////////////////////////////////////////////////////////////
67 /// <summary>
68 /// Returns all the attributes of type T of an enumeration.
69 /// </summary>
70 /// <typeparam name="T">the attribute to retrieve</typeparam>
71 /// <returns>a list of attributes</returns>
72 public static IEnumerable<T> wasGetEnumAttributes<T>(Enum e) where T : Attribute
73 {
74 return e.GetType().GetRuntimeFields()
75 .AsParallel().Select(o => wasGetAttributeFromEnumValue<T>((Enum)o.GetValue(Activator.CreateInstance<T>())));
76 }
77  
78 ///////////////////////////////////////////////////////////////////////////
79 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
80 ///////////////////////////////////////////////////////////////////////////
81 /// <summary>
82 /// Returns all the field names of an enumeration.
83 /// </summary>
84 /// <returns>the field names</returns>
85 public static IEnumerable<string> wasGetEnumNames<T>()
86 {
87 return
88 typeof (T).GetRuntimeFields()
89 .AsParallel()
90 .Select(o => o.GetCustomAttribute(typeof (NameAttribute), false))
91 .Select(o => (o as NameAttribute)?.Name)
92 .Where(o => !string.IsNullOrEmpty(o))
93 .Select(o => o);
94 }
95  
96 ///////////////////////////////////////////////////////////////////////////
97 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
98 ///////////////////////////////////////////////////////////////////////////
99 /// <summary>
100 /// Returns all the values of an enumeration.
101 /// </summary>
102 /// <returns>the values of the enumeration</returns>
103 public static IEnumerable<T> wasGetEnumValues<T>()
104 {
105 return Enum.GetValues(typeof (T)).Cast<object>().Select(value => (T) value);
106 }
107  
108 ///////////////////////////////////////////////////////////////////////////
109 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
110 ///////////////////////////////////////////////////////////////////////////
111 /// <summary>
112 /// Get the description from an enumeration value.
113 /// </summary>
114 /// <param name="value">an enumeration value</param>
115 /// <returns>the description or the empty string</returns>
116 public static string wasGetNameFromEnumValue(Enum value)
117 {
118 NameAttribute attribute = value.GetType()
119 .GetRuntimeField(value.ToString())
120 .GetCustomAttributes(typeof(NameAttribute), false)
121 .SingleOrDefault() as NameAttribute;
122 return attribute?.Name;
123 }
124  
125 ///////////////////////////////////////////////////////////////////////////
126 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
127 ///////////////////////////////////////////////////////////////////////////
128 /// <summary>
129 /// Get the description from an enumeration value.
130 /// </summary>
131 /// <param name="value">an enumeration value</param>
132 /// <returns>the description or the empty string</returns>
133 public static string wasGetDescriptionFromEnumValue(Enum value)
134 {
135 DescriptionAttribute attribute = value.GetType()
136 .GetRuntimeField(value.ToString())
137 .GetCustomAttributes(typeof(DescriptionAttribute), false)
138 .SingleOrDefault() as DescriptionAttribute;
139 return attribute?.Description;
140 }
141  
142 ///////////////////////////////////////////////////////////////////////////
143 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
144 ///////////////////////////////////////////////////////////////////////////
145 /// <summary>
146 /// Get enumeration value from its name attribute.
147 /// </summary>
148 /// <typeparam name="T">the enumeration type</typeparam>
149 /// <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>
151 public static T wasGetEnumValueFromName<T>(string name)
152 {
153 var field = typeof(T).GetRuntimeFields()
154 .AsParallel().SelectMany(f => f.GetCustomAttributes(
155 typeof(NameAttribute), false), (
156 f, a) => new { Field = f, Att = a }).SingleOrDefault(a => ((NameAttribute)a.Att)
157 .Name.Equals(name));
158 return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
159 }
160  
161 ///////////////////////////////////////////////////////////////////////////
162 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
163 ///////////////////////////////////////////////////////////////////////////
164 /// <summary>
165 /// Get the description of structure member.
166 /// </summary>
167 /// <typeparam name="T">the type of the structure to search</typeparam>
168 /// <param name="structure">the structure to search</param>
169 /// <param name="item">the value of the item to search</param>
170 /// <returns>the description or the empty string</returns>
171 public static string wasGetStructureMemberName<T>(T structure, object item) where T : struct
172 {
173 var field = typeof(T).GetRuntimeFields()
174 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false),
175 (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;
177 }
178 }
179 }