1
+ using System ;
2
+ using System . Reflection ;
3
+
4
+ #if REFLECTIONBRIDGE
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
7
+ #endif
8
+
9
+ namespace ReflectionBridge . Extensions
10
+ {
11
+ public static class ReflectionBridgeExtensions
12
+ {
13
+ public static Assembly GetAssembly ( this Type type )
14
+ {
15
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
16
+ return type . GetTypeInfo ( ) . Assembly ;
17
+ #else
18
+ return type . Assembly ;
19
+ #endif
20
+ }
21
+
22
+ public static bool IsSealed ( this Type type )
23
+ {
24
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
25
+ return type . GetTypeInfo ( ) . IsSealed ;
26
+ #else
27
+ return type . IsSealed ;
28
+ #endif
29
+ }
30
+
31
+ public static bool IsAbstract ( this Type type )
32
+ {
33
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
34
+ return type . GetTypeInfo ( ) . IsAbstract ;
35
+ #else
36
+ return type . IsAbstract ;
37
+ #endif
38
+ }
39
+
40
+ public static bool IsEnum ( this Type type )
41
+ {
42
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
43
+ return type . GetTypeInfo ( ) . IsEnum ;
44
+ #else
45
+ return type . IsEnum ;
46
+ #endif
47
+ }
48
+
49
+ public static bool IsClass ( this Type type )
50
+ {
51
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
52
+ return type . GetTypeInfo ( ) . IsClass ;
53
+ #else
54
+ return type . IsClass ;
55
+ #endif
56
+ }
57
+
58
+ public static bool IsPrimitive ( this Type type )
59
+ {
60
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
61
+ return type . GetTypeInfo ( ) . IsPrimitive ;
62
+ #else
63
+ return type . IsPrimitive ;
64
+ #endif
65
+ }
66
+
67
+ public static bool IsPublic ( this Type type )
68
+ {
69
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
70
+ return type . GetTypeInfo ( ) . IsPublic ;
71
+ #else
72
+ return type . IsPublic ;
73
+ #endif
74
+ }
75
+
76
+ public static bool IsNestedPublic ( this Type type )
77
+ {
78
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
79
+ return type . GetTypeInfo ( ) . IsNestedPublic ;
80
+ #else
81
+ return type . IsNestedPublic ;
82
+ #endif
83
+ }
84
+
85
+ public static bool IsFromLocalAssembly ( this Type type )
86
+ {
87
+ #if SILVERLIGHT
88
+ string assemblyName = type . GetAssembly ( ) . FullName ;
89
+ #else
90
+ string assemblyName = type . GetAssembly ( ) . GetName ( ) . Name ;
91
+ #endif
92
+
93
+ try
94
+ {
95
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
96
+ Assembly . Load ( new AssemblyName { Name = assemblyName } ) ;
97
+ #else
98
+ Assembly . Load ( assemblyName ) ;
99
+ #endif
100
+ return true ;
101
+ }
102
+ catch
103
+ {
104
+ return false ;
105
+ }
106
+ }
107
+
108
+ public static bool IsGenericType ( this Type type )
109
+ {
110
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
111
+ return type . GetTypeInfo ( ) . IsGenericType ;
112
+ #else
113
+ return type . IsGenericType ;
114
+ #endif
115
+ }
116
+
117
+ public static bool IsInterface ( this Type type )
118
+ {
119
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
120
+ return type . GetTypeInfo ( ) . IsInterface ;
121
+ #else
122
+ return type . IsInterface ;
123
+ #endif
124
+ }
125
+
126
+ public static Type BaseType ( this Type type )
127
+ {
128
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
129
+ return type . GetTypeInfo ( ) . BaseType ;
130
+ #else
131
+ return type . BaseType ;
132
+ #endif
133
+ }
134
+
135
+ public static bool IsValueType ( this Type type )
136
+ {
137
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
138
+ return type . GetTypeInfo ( ) . IsValueType ;
139
+ #else
140
+ return type . IsValueType ;
141
+ #endif
142
+ }
143
+
144
+ public static T GetPropertyValue < T > ( this Type type , string propertyName , object target )
145
+ {
146
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
147
+ PropertyInfo property = type . GetTypeInfo ( ) . GetDeclaredProperty ( propertyName ) ;
148
+ return ( T ) property . GetValue ( target ) ;
149
+ #else
150
+ return ( T ) type . InvokeMember ( propertyName , BindingFlags . GetProperty , null , target , null ) ;
151
+ #endif
152
+ }
153
+
154
+ public static void SetPropertyValue ( this Type type , string propertyName , object target , object value )
155
+ {
156
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
157
+ PropertyInfo property = type . GetTypeInfo ( ) . GetDeclaredProperty ( propertyName ) ;
158
+ property . SetValue ( target , value ) ;
159
+ #else
160
+ type . InvokeMember ( propertyName , BindingFlags . SetProperty , null , target , new object [ ] { value } ) ;
161
+ #endif
162
+ }
163
+
164
+ public static void SetFieldValue ( this Type type , string fieldName , object target , object value )
165
+ {
166
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
167
+ FieldInfo field = type . GetTypeInfo ( ) . GetDeclaredField ( fieldName ) ;
168
+ if ( field != null )
169
+ {
170
+ field . SetValue ( target , value ) ;
171
+ }
172
+ else
173
+ {
174
+ type . SetPropertyValue ( fieldName , target , value ) ;
175
+ }
176
+ #else
177
+ type . InvokeMember ( fieldName , BindingFlags . SetField | BindingFlags . SetProperty , null , target , new object [ ] { value } ) ;
178
+ #endif
179
+ }
180
+
181
+ public static void InvokeMethod < T > ( this Type type , string methodName , object target , T value )
182
+ {
183
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
184
+ MethodInfo method = type . GetTypeInfo ( ) . GetDeclaredMethod ( methodName ) ;
185
+ method . Invoke ( target , new object [ ] { value } ) ;
186
+ #else
187
+ type . InvokeMember ( methodName , BindingFlags . InvokeMethod , null , target , new object [ ] { value } ) ;
188
+ #endif
189
+ }
190
+
191
+ #if REFLECTIONBRIDGE && ( ! ( NET40 || NET35 || NET20 || SILVERLIGHT ) )
192
+ public static IEnumerable < MethodInfo > GetMethods ( this Type someType )
193
+ {
194
+ var t = someType ;
195
+ while ( t != null )
196
+ {
197
+ var ti = t . GetTypeInfo ( ) ;
198
+ foreach ( var m in ti . DeclaredMethods )
199
+ yield return m ;
200
+ t = ti . BaseType ;
201
+ }
202
+ }
203
+
204
+ public static Type [ ] GetGenericArguments ( this Type type )
205
+ {
206
+ return type . GetTypeInfo ( ) . GenericTypeArguments ;
207
+ }
208
+
209
+ /*
210
+ public static bool IsAssignableFrom(this Type type, Type otherType)
211
+ {
212
+ return type.GetTypeInfo().IsAssignableFrom(otherType.GetTypeInfo());
213
+ }*/
214
+
215
+ public static bool IsSubclassOf ( this Type type , Type c )
216
+ {
217
+ return type . GetTypeInfo ( ) . IsSubclassOf ( c ) ;
218
+ }
219
+
220
+ public static Attribute [ ] GetCustomAttributes ( this Type type )
221
+ {
222
+ return type . GetTypeInfo ( ) . GetCustomAttributes ( ) . ToArray ( ) ;
223
+ }
224
+
225
+ public static Attribute [ ] GetCustomAttributes ( this Type type , Type attributeType , bool inherit )
226
+ {
227
+ return type . GetTypeInfo ( ) . GetCustomAttributes ( attributeType , inherit ) . Cast < Attribute > ( ) . ToArray ( ) ;
228
+ }
229
+ #endif
230
+ }
231
+ }
0 commit comments