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