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