wasSharp – Diff between revs 26 and 27
?pathlinks?
Rev 26 | Rev 27 | |||
---|---|---|---|---|
Line 20... | Line 20... | |||
20 | /// Retrieves an attribute of type T from an enumeration. |
20 | /// Retrieves an attribute of type T from an enumeration. |
|
21 | /// </summary> |
21 | /// </summary> |
|
22 | /// <returns>an attribute of type T</returns> |
22 | /// <returns>an attribute of type T</returns> |
|
23 | public static T GetAttributeFromEnumValue<T>(Enum value) where T : Attribute |
23 | public static T GetAttributeFromEnumValue<T>(Enum value) where T : Attribute |
|
24 | { |
24 | { |
|
25 | return (T) value.GetType() |
25 | return (T)value.GetType() |
|
26 | .GetRuntimeField(value.ToString()) |
26 | .GetRuntimeField(value.ToString()) |
|
27 | .GetCustomAttributes(typeof(T), false) |
27 | .GetCustomAttributes(typeof(T), false) |
|
28 | .SingleOrDefault(); |
28 | .SingleOrDefault(); |
|
29 | } |
29 | } |
|
Line 38... | Line 38... | |||
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().ToArray() |
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 | |
|
46 | /////////////////////////////////////////////////////////////////////////// |
46 | /////////////////////////////////////////////////////////////////////////// |
|
47 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
47 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
Line 68... | Line 68... | |||
68 | /// Returns all the values of an enumeration. |
68 | /// Returns all the values of an enumeration. |
|
69 | /// </summary> |
69 | /// </summary> |
|
70 | /// <returns>the values of the enumeration</returns> |
70 | /// <returns>the values of the enumeration</returns> |
|
71 | public static IEnumerable<T> GetEnumValues<T>() |
71 | public static IEnumerable<T> GetEnumValues<T>() |
|
72 | { |
72 | { |
|
73 | return Enum.GetValues(typeof(T)).Cast<object>().Select(value => (T) value); |
73 | return Enum.GetValues(typeof(T)).Cast<object>().Select(value => (T)value); |
|
74 | } |
74 | } |
|
Line 75... | Line 75... | |||
75 | |
75 | |
|
76 | /////////////////////////////////////////////////////////////////////////// |
76 | /////////////////////////////////////////////////////////////////////////// |
|
77 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
77 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
Line 121... | Line 121... | |||
121 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
121 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
|
122 | { |
122 | { |
|
123 | var field = typeof(T).GetRuntimeFields().ToArray() |
123 | var field = typeof(T).GetRuntimeFields().ToArray() |
|
124 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
124 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
|
125 | typeof(NameAttribute), false), ( |
125 | typeof(NameAttribute), false), ( |
|
126 | f, a) => new {Field = f, Att = a}) |
126 | f, a) => new { Field = f, Att = a }) |
|
127 | .SingleOrDefault(a => String.Equals(((NameAttribute) a.Att) |
127 | .SingleOrDefault(a => string.Equals(((NameAttribute)a.Att) |
|
128 | .Name, name, comparison)); |
128 | .Name, name, comparison)); |
|
129 | return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
129 | return field != null ? (T)field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
|
130 | } |
130 | } |
|
Line 131... | Line 131... | |||
131 | |
131 | |
|
132 | /////////////////////////////////////////////////////////////////////////// |
132 | /////////////////////////////////////////////////////////////////////////// |
|
133 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
133 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
Line 141... | Line 141... | |||
141 | /// <returns>the description or the empty string</returns> |
141 | /// <returns>the description or the empty string</returns> |
|
142 | public static string GetStructureMemberName<T>(T structure, object item) where T : struct |
142 | public static string GetStructureMemberName<T>(T structure, object item) where T : struct |
|
143 | { |
143 | { |
|
144 | var field = typeof(T).GetRuntimeFields().ToArray() |
144 | var field = typeof(T).GetRuntimeFields().ToArray() |
|
145 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false), |
145 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof(NameAttribute), false), |
|
146 | (f, a) => new {Field = f, Att = a}).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item)); |
146 | (f, a) => new { Field = f, Att = a }).SingleOrDefault(f => f.Field.GetValue(structure).Equals(item)); |
|
147 | return field != null ? ((NameAttribute) field.Att).Name : string.Empty; |
147 | return field != null ? ((NameAttribute)field.Att).Name : string.Empty; |
|
148 | } |
148 | } |
|
Line 149... | Line 149... | |||
149 | |
149 | |
|
150 | /////////////////////////////////////////////////////////////////////////// |
150 | /////////////////////////////////////////////////////////////////////////// |
|
151 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
151 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
Line 283... | Line 283... | |||
283 | } |
283 | } |
|
Line 284... | Line 284... | |||
284 | |
284 | |
|
285 | public string Description => description; |
285 | public string Description => description; |
|
286 | } |
286 | } |
|
287 | } |
- | ||
288 | } |
287 | } |
|
- | 288 | } |