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