wasSharp – Diff between revs 5 and 7
?pathlinks?
Rev 5 | Rev 7 | |||
---|---|---|---|---|
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 10... | Line 11... | |||
10 | using System.Reflection; |
11 | using wasSharp; |
|
11 | |
12 | |
|
12 | namespace wasSharp |
13 | namespace wasSharp |
|
13 | { |
14 | { |
|
14 | public class Reflection |
15 | public static class Reflection |
|
15 | { |
16 | { |
|
16 | /////////////////////////////////////////////////////////////////////////// |
17 | /////////////////////////////////////////////////////////////////////////// |
|
17 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
18 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
Line 36... | Line 37... | |||
36 | /// </summary> |
37 | /// </summary> |
|
37 | /// <typeparam name="T">the attribute to retrieve</typeparam> |
38 | /// <typeparam name="T">the attribute to retrieve</typeparam> |
|
38 | /// <returns>a list of attributes</returns> |
39 | /// <returns>a list of attributes</returns> |
|
39 | public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute |
40 | public static IEnumerable<T> GetEnumAttributes<T>(Enum e) where T : Attribute |
|
40 | { |
41 | { |
|
41 | return e.GetType().GetRuntimeFields() |
42 | return e.GetType().GetRuntimeFields().ToArray() |
|
42 | .AsParallel() |
43 | .AsParallel() |
|
43 | .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>()))); |
44 | .Select(o => GetAttributeFromEnumValue<T>((Enum) o.GetValue(Activator.CreateInstance<T>()))); |
|
44 | } |
45 | } |
|
Line 45... | Line 46... | |||
45 | |
46 | |
|
Line 51... | Line 52... | |||
51 | /// </summary> |
52 | /// </summary> |
|
52 | /// <returns>the field names</returns> |
53 | /// <returns>the field names</returns> |
|
53 | public static IEnumerable<string> GetEnumNames<T>() |
54 | public static IEnumerable<string> GetEnumNames<T>() |
|
54 | { |
55 | { |
|
55 | return |
56 | return |
|
56 | typeof (T).GetRuntimeFields() |
57 | typeof (T).GetRuntimeFields().ToArray() |
|
57 | .AsParallel() |
58 | .AsParallel() |
|
58 | .Select(o => o.GetCustomAttribute(typeof (NameAttribute), false)) |
59 | .Select(o => o.GetCustomAttribute(typeof (NameAttribute), false)) |
|
59 | .Select(o => (o as NameAttribute)?.Name) |
60 | .Select(o => (o as NameAttribute)?.Name) |
|
60 | .Where(o => !string.IsNullOrEmpty(o)) |
61 | .Where(o => !string.IsNullOrEmpty(o)) |
|
61 | .Select(o => o); |
62 | .Select(o => o); |
|
Line 75... | Line 76... | |||
75 | |
76 | |
|
76 | /////////////////////////////////////////////////////////////////////////// |
77 | /////////////////////////////////////////////////////////////////////////// |
|
77 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
78 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
78 | /////////////////////////////////////////////////////////////////////////// |
79 | /////////////////////////////////////////////////////////////////////////// |
|
79 | /// <summary> |
80 | /// <summary> |
|
80 | /// Get the description from an enumeration value. |
81 | /// Get the name from an enumeration value. |
|
81 | /// </summary> |
82 | /// </summary> |
|
82 | /// <param name="value">an enumeration value</param> |
83 | /// <param name="value">an enumeration value</param> |
|
83 | /// <returns>the description or the empty string</returns> |
84 | /// <returns>the description or the empty string</returns> |
|
84 | public static string GetNameFromEnumValue(Enum value) |
85 | public static string GetNameFromEnumValue(Enum value) |
|
85 | { |
86 | { |
|
86 | NameAttribute attribute = value.GetType() |
87 | var attribute = value.GetType() |
|
87 | .GetRuntimeField(value.ToString()) |
88 | .GetRuntimeField(value.ToString()) |
|
88 | .GetCustomAttributes(typeof (NameAttribute), false) |
89 | .GetCustomAttributes(typeof (NameAttribute), false) |
|
89 | .SingleOrDefault() as NameAttribute; |
90 | .SingleOrDefault() as NameAttribute; |
|
90 | return attribute?.Name; |
91 | return attribute?.Name; |
|
Line 98... | Line 99... | |||
98 | /// </summary> |
99 | /// </summary> |
|
99 | /// <param name="value">an enumeration value</param> |
100 | /// <param name="value">an enumeration value</param> |
|
100 | /// <returns>the description or the empty string</returns> |
101 | /// <returns>the description or the empty string</returns> |
|
101 | public static string GetDescriptionFromEnumValue(Enum value) |
102 | public static string GetDescriptionFromEnumValue(Enum value) |
|
102 | { |
103 | { |
|
103 | DescriptionAttribute attribute = value.GetType() |
104 | var attribute = value.GetType() |
|
104 | .GetRuntimeField(value.ToString()) |
105 | .GetRuntimeField(value.ToString()) |
|
105 | .GetCustomAttributes(typeof (DescriptionAttribute), false) |
106 | .GetCustomAttributes(typeof (DescriptionAttribute), false) |
|
106 | .SingleOrDefault() as DescriptionAttribute; |
107 | .SingleOrDefault() as DescriptionAttribute; |
|
107 | return attribute?.Description; |
108 | return attribute?.Description; |
|
108 | } |
109 | } |
|
Line 116... | Line 117... | |||
116 | /// <typeparam name="T">the enumeration type</typeparam> |
117 | /// <typeparam name="T">the enumeration type</typeparam> |
|
117 | /// <param name="name">the description of a member</param> |
118 | /// <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> |
119 | /// <returns>the value or the default of T if case no name attribute found</returns> |
|
119 | public static T GetEnumValueFromName<T>(string name) |
120 | public static T GetEnumValueFromName<T>(string name) |
|
120 | { |
121 | { |
|
121 | var field = typeof (T).GetRuntimeFields() |
122 | var field = typeof (T).GetRuntimeFields().ToArray() |
|
122 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
123 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
|
123 | typeof (NameAttribute), false), ( |
124 | typeof (NameAttribute), false), ( |
|
124 | f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((NameAttribute) a.Att) |
125 | f, a) => new {Field = f, Att = a}).SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att) |
|
125 | .Name.Equals(name)); |
126 | .Name, name, StringComparison.Ordinal)); |
|
126 | 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); |
|
127 | } |
128 | } |
|
Line 128... | Line 129... | |||
128 | |
129 | |
|
129 | /////////////////////////////////////////////////////////////////////////// |
130 | /////////////////////////////////////////////////////////////////////////// |
|
130 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
131 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
131 | /////////////////////////////////////////////////////////////////////////// |
132 | /////////////////////////////////////////////////////////////////////////// |
|
132 | /// <summary> |
133 | /// <summary> |
|
133 | /// Get the description of structure member. |
134 | /// Get the name of a structure member. |
|
134 | /// </summary> |
135 | /// </summary> |
|
135 | /// <typeparam name="T">the type of the structure to search</typeparam> |
136 | /// <typeparam name="T">the type of the structure to search</typeparam> |
|
136 | /// <param name="structure">the structure to search</param> |
137 | /// <param name="structure">the structure to search</param> |
|
137 | /// <param name="item">the value of the item to search</param> |
138 | /// <param name="item">the value of the item to search</param> |
|
138 | /// <returns>the description or the empty string</returns> |
139 | /// <returns>the description or the empty string</returns> |
|
139 | 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 |
|
140 | { |
141 | { |
|
141 | var field = typeof (T).GetRuntimeFields() |
142 | var field = typeof (T).GetRuntimeFields().ToArray() |
|
142 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false), |
143 | .AsParallel().SelectMany(f => f.GetCustomAttributes(typeof (NameAttribute), false), |
|
143 | (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)); |
|
144 | return field != null ? ((NameAttribute) field.Att).Name : string.Empty; |
145 | return field != null ? ((NameAttribute) field.Att).Name : string.Empty; |
|
Line -... | Line 146... | |||
- | 146 | } |
||
- | 147 | |
||
- | 148 | /////////////////////////////////////////////////////////////////////////// |
||
- | 149 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 150 | /////////////////////////////////////////////////////////////////////////// |
||
- | 151 | /// <summary> |
||
- | 152 | /// Get field or property from a class by supplying a path. |
||
- | 153 | /// </summary> |
||
- | 154 | /// <typeparam name="T">the type of the object</typeparam> |
||
- | 155 | /// <param name="o">the object</param> |
||
- | 156 | /// <param name="path">the fully qualified path to the field of property</param> |
||
- | 157 | /// <returns> |
||
- | 158 | /// the last object in the fully qualified path or null in case the field or property could not be found |
||
- | 159 | /// </returns> |
||
- | 160 | public static object GetFP<T>(this T o, string path) |
||
- | 161 | { |
||
- | 162 | if (string.IsNullOrEmpty(path)) return null; |
||
- | 163 | if (o == null) return null; |
||
- | 164 | |
||
- | 165 | var memberType = o.GetType(); |
||
- | 166 | var components = path.Split('.'); |
||
- | 167 | |
||
- | 168 | var f = memberType.GetRuntimeField(components[0]); |
||
- | 169 | var p = memberType.GetRuntimeProperty(components[0]); |
||
- | 170 | |
||
- | 171 | if (f != null) |
||
- | 172 | return components.Length > 1 |
||
- | 173 | ? GetFP(f.GetValue(o), |
||
- | 174 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
- | 175 | : memberType.GetRuntimeField(path).GetValue(o); |
||
- | 176 | |
||
- | 177 | if (p != null) |
||
- | 178 | return components.Length > 1 |
||
- | 179 | ? GetFP(p.GetValue(o), |
||
- | 180 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
- | 181 | : memberType.GetRuntimeProperty(path).GetValue(o); |
||
- | 182 | |
||
- | 183 | return null; |
||
- | 184 | } |
||
- | 185 | |
||
- | 186 | /////////////////////////////////////////////////////////////////////////// |
||
- | 187 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 188 | /////////////////////////////////////////////////////////////////////////// |
||
- | 189 | /// <summary> |
||
- | 190 | /// Get field or property info from a class by supplying a path. |
||
- | 191 | /// </summary> |
||
- | 192 | /// <typeparam name="T">the type of the object</typeparam> |
||
- | 193 | /// <param name="o">the object</param> |
||
- | 194 | /// <param name="path">the fully qualified path to the field of property</param> |
||
- | 195 | /// <returns> |
||
- | 196 | /// the field or property info of the last object in the path or null if the object cannot be found |
||
- | 197 | /// </returns> |
||
- | 198 | public static object GetFPInfo<T>(this T o, string path) |
||
- | 199 | { |
||
- | 200 | if (string.IsNullOrEmpty(path)) return null; |
||
- | 201 | if (o == null) return null; |
||
- | 202 | |
||
- | 203 | var memberType = o.GetType(); |
||
- | 204 | var components = path.Split('.'); |
||
- | 205 | |
||
- | 206 | var f = memberType.GetRuntimeField(components[0]); |
||
- | 207 | var p = memberType.GetRuntimeProperty(components[0]); |
||
- | 208 | |
||
- | 209 | if (f != null) |
||
- | 210 | return components.Length > 1 |
||
- | 211 | ? GetFPInfo(f.GetValue(o), |
||
- | 212 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
- | 213 | : memberType.GetRuntimeField(path); |
||
- | 214 | |
||
- | 215 | if (p != null) |
||
- | 216 | return components.Length > 1 |
||
- | 217 | ? GetFPInfo(p.GetValue(o), |
||
- | 218 | components.Skip(1).Aggregate((a, i) => a + @"." + i)) |
||
- | 219 | : memberType.GetRuntimeProperty(path); |
||
- | 220 | |
||
- | 221 | return null; |
||
- | 222 | } |
||
- | 223 | |
||
- | 224 | /////////////////////////////////////////////////////////////////////////// |
||
- | 225 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 226 | /////////////////////////////////////////////////////////////////////////// |
||
- | 227 | /// <summary> |
||
- | 228 | /// Enumerate all the base types recursively starting from a type. |
||
- | 229 | /// </summary> |
||
- | 230 | /// <param name="type">the type</param> |
||
- | 231 | /// <returns>an enumeration of all base types</returns> |
||
- | 232 | public static IEnumerable<Type> GetBaseTypes(this Type type) |
||
- | 233 | { |
||
- | 234 | var baseType = type.GetTypeInfo().BaseType; |
||
- | 235 | if(baseType == null) |
||
- | 236 | yield break; |
||
- | 237 | yield return baseType; |
||
- | 238 | foreach (var t in GetBaseTypes(baseType)) |
||
- | 239 | { |
||
- | 240 | yield return t; |
||
- | 241 | } |
||
145 | } |
242 | } |
|
146 | |
243 | |
|
147 | /// <summary> |
244 | /// <summary> |
|
148 | /// A generic name attribute. |
245 | /// A generic name attribute. |
|
149 | /// </summary> |
246 | /// </summary> |