corrade-vassal – Diff between revs 13 and 16

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 13 Rev 16
Line 9... Line 9...
9 using System.Linq; 9 using System.Linq;
10 using System.Reflection; 10 using System.Reflection;
Line 11... Line 11...
11   11  
12 namespace wasSharp 12 namespace wasSharp
13 { 13 {
14 public 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 ///////////////////////////////////////////////////////////////////////////
Line 36... Line 36...
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() 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 }
Line 45... Line 45...
45   45  
Line 51... Line 51...
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() 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);
Line 81... Line 81...
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 NameAttribute 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 }
Line 98... Line 98...
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 DescriptionAttribute 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 }
Line 116... Line 116...
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 /// <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>
119 public static T GetEnumValueFromName<T>(string name) 119 public static T GetEnumValueFromName<T>(string name)
120 { 120 {
121 var field = typeof (T).GetRuntimeFields() 121 var field = typeof (T).GetRuntimeFields().ToArray()
122 .AsParallel().SelectMany(f => f.GetCustomAttributes( 122 .AsParallel().SelectMany(f => f.GetCustomAttributes(
123 typeof (NameAttribute), false), ( 123 typeof (NameAttribute), false), (
124 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)
125 .Name.Equals(name)); 125 .Name.Equals(name));
126 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);
Line 128... Line 128...
128   128  
129 /////////////////////////////////////////////////////////////////////////// 129 ///////////////////////////////////////////////////////////////////////////
130 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 130 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
131 /////////////////////////////////////////////////////////////////////////// 131 ///////////////////////////////////////////////////////////////////////////
132 /// <summary> 132 /// <summary>
133 /// Get the description of structure member. 133 /// Get the name of a structure member.
134 /// </summary> 134 /// </summary>
135 /// <typeparam name="T">the type of the structure to search</typeparam> 135 /// <typeparam name="T">the type of the structure to search</typeparam>
136 /// <param name="structure">the structure to search</param> 136 /// <param name="structure">the structure to search</param>
137 /// <param name="item">the value of the item to search</param> 137 /// <param name="item">the value of the item to search</param>
138 /// <returns>the description or the empty string</returns> 138 /// <returns>the description or the empty string</returns>
139 public static string GetStructureMemberName<T>(T structure, object item) where T : struct 139 public static string GetStructureMemberName<T>(T structure, object item) where T : struct
140 { 140 {
141 var field = typeof (T).GetRuntimeFields() 141 var field = typeof (T).GetRuntimeFields().ToArray()
142 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false), 142 .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false),
143 (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));
144 return field != null ? ((NameAttribute) field.Att).Name : string.Empty; 144 return field != null ? ((NameAttribute) field.Att).Name : string.Empty;
Line -... Line 145...
-   145 }
-   146  
-   147 ///////////////////////////////////////////////////////////////////////////
-   148 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
-   149 ///////////////////////////////////////////////////////////////////////////
-   150 /// <summary>
-   151 /// Get enumeration value from its description.
-   152 /// </summary>
-   153 /// <typeparam name="T">the enumeration type</typeparam>
-   154 /// <param name="description">the description of a member</param>
-   155 /// <returns>the value or the default of T if case no description found</returns>
-   156 public static T GetEnumValueFromDescription<T>(string description)
-   157 {
-   158 var field = typeof(T).GetRuntimeFields().ToArray()
-   159 .AsParallel().SelectMany(f => f.GetCustomAttributes(
-   160 typeof(DescriptionAttribute), false), (
-   161 f, a) => new { Field = f, Att = a }).SingleOrDefault(a => ((DescriptionAttribute)a.Att)
-   162 .Description.Equals(description));
-   163 return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T);
145 } 164 }
146   165  
147 /// <summary> 166 /// <summary>
148 /// A generic name attribute. 167 /// A generic name attribute.
149 /// </summary> 168 /// </summary>