wasSharpNET – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2016 - 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 | |||
12 | namespace wasSharpNET |
||
13 | { |
||
14 | public static class Reflection |
||
15 | { |
||
16 | /////////////////////////////////////////////////////////////////////////// |
||
17 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
18 | /////////////////////////////////////////////////////////////////////////// |
||
19 | /// <summary> |
||
20 | /// Enumerates the fields of an object along with the child objects, |
||
21 | /// provided that all child objects are part of a specified namespace. |
||
22 | /// </summary> |
||
23 | /// <param name="object">the object to enumerate</param> |
||
24 | /// <param name="namespace">the namespace to enumerate in</param> |
||
25 | /// <returns>child objects of the object</returns> |
||
26 | public static IEnumerable<KeyValuePair<FieldInfo, object>> wasGetFields(object @object, string @namespace) |
||
27 | { |
||
28 | if (@object == null) yield break; |
||
29 | |||
30 | foreach (var fi in @object.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) |
||
31 | { |
||
32 | if (fi.FieldType.FullName.Split('.', '+') |
||
33 | .Contains(@namespace, StringComparer.OrdinalIgnoreCase)) |
||
34 | foreach (var sf in wasGetFields(fi.GetValue(@object), @namespace)) |
||
35 | yield return sf; |
||
36 | yield return new KeyValuePair<FieldInfo, object>(fi, @object); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /////////////////////////////////////////////////////////////////////////// |
||
41 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
42 | /////////////////////////////////////////////////////////////////////////// |
||
43 | /// <summary> |
||
44 | /// Enumerates the properties of an object along with the child objects, |
||
45 | /// provided that all child objects are part of a specified namespace. |
||
46 | /// </summary> |
||
47 | /// <param name="object">the object to enumerate</param> |
||
48 | /// <param name="namespace">the namespace to enumerate in</param> |
||
49 | /// <returns>child objects of the object</returns> |
||
50 | public static IEnumerable<KeyValuePair<PropertyInfo, object>> wasGetProperties(object @object, |
||
51 | string @namespace) |
||
52 | { |
||
53 | if (@object == null) yield break; |
||
54 | |||
55 | foreach (var pi in @object.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) |
||
56 | { |
||
57 | if (pi.PropertyType.FullName.Split('.', '+') |
||
58 | .Contains(@namespace, StringComparer.OrdinalIgnoreCase)) |
||
59 | { |
||
60 | var getMethod = pi.GetGetMethod(); |
||
61 | if (getMethod.ReturnType.IsArray) |
||
62 | { |
||
27 | office | 63 | var array = (Array) getMethod.Invoke(@object, null); |
1 | office | 64 | foreach (var sp in |
65 | array.Cast<object>().SelectMany(element => wasGetProperties(element, @namespace))) |
||
66 | yield return sp; |
||
67 | } |
||
68 | foreach ( |
||
69 | var sp in |
||
27 | office | 70 | wasGetProperties(pi.GetValue(@object, null), @namespace)) |
1 | office | 71 | yield return sp; |
72 | } |
||
73 | yield return new KeyValuePair<PropertyInfo, object>(pi, @object); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /////////////////////////////////////////////////////////////////////////// |
||
78 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
79 | /////////////////////////////////////////////////////////////////////////// |
||
80 | /// <summary> |
||
81 | /// This is a wrapper for both FieldInfo and PropertyInfo SetValue. |
||
82 | /// </summary> |
||
83 | /// <param name="info">either a FieldInfo or PropertyInfo</param> |
||
84 | /// <param name="object">the object to set the value on</param> |
||
85 | /// <param name="value">the value to set</param> |
||
86 | public static void wasSetInfoValue<TK, TV>(TK info, ref TV @object, object value) |
||
87 | { |
||
88 | object o = @object; |
||
27 | office | 89 | var fi = (object) info as FieldInfo; |
1 | office | 90 | if (fi != null) |
91 | { |
||
92 | fi.SetValue(o, value); |
||
27 | office | 93 | @object = (TV) o; |
1 | office | 94 | return; |
95 | } |
||
27 | office | 96 | var pi = (object) info as PropertyInfo; |
1 | office | 97 | if (pi != null) |
98 | { |
||
99 | pi.SetValue(o, value, null); |
||
27 | office | 100 | @object = (TV) o; |
1 | office | 101 | } |
102 | } |
||
103 | |||
104 | /////////////////////////////////////////////////////////////////////////// |
||
105 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
106 | /////////////////////////////////////////////////////////////////////////// |
||
107 | /// <summary> |
||
108 | /// This is a wrapper for both FieldInfo and PropertyInfo GetValue. |
||
109 | /// </summary> |
||
110 | /// <param name="info">either a FieldInfo or PropertyInfo</param> |
||
111 | /// <param name="value">the object to get from</param> |
||
112 | /// <returns>the value of the field or property</returns> |
||
113 | public static object wasGetInfoValue<T>(T info, object value) |
||
114 | { |
||
27 | office | 115 | var fi = (object) info as FieldInfo; |
1 | office | 116 | if (fi != null) |
117 | return fi.GetValue(value); |
||
27 | office | 118 | var pi = (object) info as PropertyInfo; |
1 | office | 119 | if (pi != null) |
120 | { |
||
121 | if (pi.GetIndexParameters().Any()) |
||
122 | return value; |
||
123 | return pi.GetValue(value, null); |
||
124 | } |
||
125 | return null; |
||
126 | } |
||
127 | } |
||
27 | office | 128 | } |