wasSharp – Diff between revs 7 and 10

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 7 Rev 10
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; -  
Line 11... Line 10...
11 using wasSharp; 10 using System.Reflection;
12   11  
13 namespace wasSharp 12 namespace wasSharp
14 { 13 {
Line 23... Line 22...
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 }
Line 31... Line 30...
31   30  
32 /////////////////////////////////////////////////////////////////////////// 31 ///////////////////////////////////////////////////////////////////////////
Line 52... Line 51...
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 }
Line 69... Line 68...
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 }
Line 76... Line 75...
76   75  
77 /////////////////////////////////////////////////////////////////////////// 76 ///////////////////////////////////////////////////////////////////////////
78 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 77 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
Line 84... Line 83...
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 }
Line 93... Line 92...
93   92  
Line 101... Line 100...
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 }
Line 110... Line 109...
110   109  
Line 117... Line 116...
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 }
Line 129... Line 129...
129   129  
130 /////////////////////////////////////////////////////////////////////////// 130 ///////////////////////////////////////////////////////////////////////////
Line 137... Line 137...
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 }
Line 147... Line 147...
147   147  
Line 230... Line 230...
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;