4 // Sean MacIsaac (macisaac@ximian.com)
5 // Paolo Molaro (lupus@ximian.com)
6 // Patrik Torstensson (patrik.torstensson@labs2.com)
8 // (C) 2001 Ximian, Inc.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Reflection
;
35 using System
.Reflection
.Emit
;
36 using System
.Collections
;
37 using System
.Runtime
.CompilerServices
;
38 using System
.Globalization
;
39 using System
.Runtime
.Serialization
;
41 namespace System
.Reflection
43 internal class MonoGenericInst
: MonoType
45 protected Type generic_type
;
49 internal MonoGenericInst ()
52 // this should not be used
53 throw new InvalidOperationException ();
56 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
57 protected extern void initialize (MethodInfo
[] methods
, ConstructorInfo
[] ctors
, FieldInfo
[] fields
, PropertyInfo
[] properties
, EventInfo
[] events
);
59 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
60 protected extern MethodInfo
[] GetMethods_internal (Type reflected_type
);
62 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
63 protected extern ConstructorInfo
[] GetConstructors_internal (Type reflected_type
);
65 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
66 protected extern FieldInfo
[] GetFields_internal (Type reflected_type
);
68 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
69 protected extern PropertyInfo
[] GetProperties_internal (Type reflected_type
);
71 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
72 protected extern EventInfo
[] GetEvents_internal (Type reflected_type
);
74 private const BindingFlags flags
= BindingFlags
.Public
| BindingFlags
.NonPublic
|
75 BindingFlags
.Static
| BindingFlags
.Instance
| BindingFlags
.DeclaredOnly
;
77 EventInfo
[] get_event_info ()
79 if (generic_type
is TypeBuilder
)
80 return ((TypeBuilder
) generic_type
).GetEvents_internal (flags
);
82 return generic_type
.GetEvents (flags
);
90 MonoGenericInst parent
= GetParentType ();
94 initialize (generic_type
.GetMethods (flags
),
95 generic_type
.GetConstructors (flags
),
96 generic_type
.GetFields (flags
),
97 generic_type
.GetProperties (flags
),
103 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
104 protected extern MonoGenericInst
GetParentType ();
106 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
107 protected extern MonoGenericInst
[] GetInterfaces_internal ();
109 public override Type BaseType
{
111 MonoGenericInst parent
= GetParentType ();
112 return parent
!= null ? parent
: generic_type
.BaseType
;
116 public override Type
[] GetInterfaces ()
118 return GetInterfaces_internal ();
121 protected override bool IsValueTypeImpl ()
123 return generic_type
.IsValueType
;
126 public override MethodInfo
[] GetMethods (BindingFlags bf
)
128 ArrayList l
= new ArrayList ();
131 // Walk up our class hierarchy and retrieve methods from our
135 Type current_type
= this;
137 MonoGenericInst gi
= current_type
as MonoGenericInst
;
139 l
.AddRange (gi
.GetMethods_impl (bf
, this));
140 else if (current_type
is TypeBuilder
)
141 l
.AddRange (current_type
.GetMethods (bf
));
143 // If we encounter a `MonoType', its
144 // GetMethodsByName() will return all the methods
145 // from its parent type(s), so we can stop here.
146 MonoType mt
= (MonoType
) current_type
;
147 l
.AddRange (mt
.GetMethodsByName (null, bf
, false, this));
151 if ((bf
& BindingFlags
.DeclaredOnly
) != 0)
153 current_type
= current_type
.BaseType
;
154 } while (current_type
!= null);
156 MethodInfo
[] result
= new MethodInfo
[l
.Count
];
161 protected MethodInfo
[] GetMethods_impl (BindingFlags bf
, Type reftype
)
163 ArrayList l
= new ArrayList ();
165 MethodAttributes mattrs
;
169 MethodInfo
[] methods
= GetMethods_internal (reftype
);
171 for (int i
= 0; i
< methods
.Length
; i
++) {
172 MethodInfo c
= methods
[i
];
175 mattrs
= c
.Attributes
;
176 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
177 if ((bf
& BindingFlags
.Public
) != 0)
180 if ((bf
& BindingFlags
.NonPublic
) != 0)
186 if ((mattrs
& MethodAttributes
.Static
) != 0) {
187 if ((bf
& BindingFlags
.Static
) != 0)
190 if ((bf
& BindingFlags
.Instance
) != 0)
197 MethodInfo
[] result
= new MethodInfo
[l
.Count
];
202 public override ConstructorInfo
[] GetConstructors (BindingFlags bf
)
204 ArrayList l
= new ArrayList ();
206 Type current_type
= this;
208 MonoGenericInst gi
= current_type
as MonoGenericInst
;
210 l
.AddRange (gi
.GetConstructors_impl (bf
, this));
211 else if (current_type
is TypeBuilder
)
212 l
.AddRange (current_type
.GetConstructors (bf
));
214 MonoType mt
= (MonoType
) current_type
;
215 l
.AddRange (mt
.GetConstructors_internal (bf
, this));
219 if ((bf
& BindingFlags
.DeclaredOnly
) != 0)
221 current_type
= current_type
.BaseType
;
222 } while (current_type
!= null);
224 ConstructorInfo
[] result
= new ConstructorInfo
[l
.Count
];
229 protected ConstructorInfo
[] GetConstructors_impl (BindingFlags bf
, Type reftype
)
231 ArrayList l
= new ArrayList ();
233 MethodAttributes mattrs
;
237 ConstructorInfo
[] ctors
= GetConstructors_internal (reftype
);
239 for (int i
= 0; i
< ctors
.Length
; i
++) {
240 ConstructorInfo c
= ctors
[i
];
243 mattrs
= c
.Attributes
;
244 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
245 if ((bf
& BindingFlags
.Public
) != 0)
248 if ((bf
& BindingFlags
.NonPublic
) != 0)
254 if ((mattrs
& MethodAttributes
.Static
) != 0) {
255 if ((bf
& BindingFlags
.Static
) != 0)
258 if ((bf
& BindingFlags
.Instance
) != 0)
266 ConstructorInfo
[] result
= new ConstructorInfo
[l
.Count
];
271 public override FieldInfo
[] GetFields (BindingFlags bf
)
273 ArrayList l
= new ArrayList ();
275 Type current_type
= this;
277 MonoGenericInst gi
= current_type
as MonoGenericInst
;
279 l
.AddRange (gi
.GetFields_impl (bf
, this));
280 else if (current_type
is TypeBuilder
)
281 l
.AddRange (current_type
.GetFields (bf
));
283 MonoType mt
= (MonoType
) current_type
;
284 l
.AddRange (mt
.GetFields_internal (bf
, this));
288 if ((bf
& BindingFlags
.DeclaredOnly
) != 0)
290 current_type
= current_type
.BaseType
;
291 } while (current_type
!= null);
293 FieldInfo
[] result
= new FieldInfo
[l
.Count
];
298 protected FieldInfo
[] GetFields_impl (BindingFlags bf
, Type reftype
)
300 ArrayList l
= new ArrayList ();
302 FieldAttributes fattrs
;
306 FieldInfo
[] fields
= GetFields_internal (reftype
);
308 for (int i
= 0; i
< fields
.Length
; i
++) {
309 FieldInfo c
= fields
[i
];
312 fattrs
= c
.Attributes
;
313 if ((fattrs
& FieldAttributes
.FieldAccessMask
) == FieldAttributes
.Public
) {
314 if ((bf
& BindingFlags
.Public
) != 0)
317 if ((bf
& BindingFlags
.NonPublic
) != 0)
323 if ((fattrs
& FieldAttributes
.Static
) != 0) {
324 if ((bf
& BindingFlags
.Static
) != 0)
327 if ((bf
& BindingFlags
.Instance
) != 0)
334 FieldInfo
[] result
= new FieldInfo
[l
.Count
];
339 public override PropertyInfo
[] GetProperties (BindingFlags bf
)
341 ArrayList l
= new ArrayList ();
343 Type current_type
= this;
345 MonoGenericInst gi
= current_type
as MonoGenericInst
;
347 l
.AddRange (gi
.GetProperties_impl (bf
, this));
348 else if (current_type
is TypeBuilder
)
349 l
.AddRange (current_type
.GetProperties (bf
));
351 MonoType mt
= (MonoType
) current_type
;
352 l
.AddRange (mt
.GetPropertiesByName (null, bf
, false, this));
356 if ((bf
& BindingFlags
.DeclaredOnly
) != 0)
358 current_type
= current_type
.BaseType
;
359 } while (current_type
!= null);
361 PropertyInfo
[] result
= new PropertyInfo
[l
.Count
];
366 protected PropertyInfo
[] GetProperties_impl (BindingFlags bf
, Type reftype
)
368 ArrayList l
= new ArrayList ();
370 MethodAttributes mattrs
;
375 PropertyInfo
[] properties
= GetProperties_internal (reftype
);
377 for (int i
= 0; i
< properties
.Length
; i
++) {
378 PropertyInfo c
= properties
[i
];
381 accessor
= c
.GetGetMethod (true);
382 if (accessor
== null)
383 accessor
= c
.GetSetMethod (true);
384 if (accessor
== null)
386 mattrs
= accessor
.Attributes
;
387 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
388 if ((bf
& BindingFlags
.Public
) != 0)
391 if ((bf
& BindingFlags
.NonPublic
) != 0)
397 if ((mattrs
& MethodAttributes
.Static
) != 0) {
398 if ((bf
& BindingFlags
.Static
) != 0)
401 if ((bf
& BindingFlags
.Instance
) != 0)
408 PropertyInfo
[] result
= new PropertyInfo
[l
.Count
];
413 public override EventInfo
[] GetEvents (BindingFlags bf
)
415 ArrayList l
= new ArrayList ();
417 Type current_type
= this;
419 MonoGenericInst gi
= current_type
as MonoGenericInst
;
421 l
.AddRange (gi
.GetEvents_impl (bf
, this));
422 else if (current_type
is TypeBuilder
)
423 l
.AddRange (current_type
.GetEvents (bf
));
425 MonoType mt
= (MonoType
) current_type
;
426 l
.AddRange (mt
.GetEvents (bf
));
430 if ((bf
& BindingFlags
.DeclaredOnly
) != 0)
432 current_type
= current_type
.BaseType
;
433 } while (current_type
!= null);
435 EventInfo
[] result
= new EventInfo
[l
.Count
];
440 protected EventInfo
[] GetEvents_impl (BindingFlags bf
, Type reftype
)
442 ArrayList l
= new ArrayList ();
444 MethodAttributes mattrs
;
449 EventInfo
[] events
= GetEvents_internal (reftype
);
451 for (int i
= 0; i
< events
.Length
; i
++) {
452 EventInfo c
= events
[i
];
455 accessor
= c
.GetAddMethod (true);
456 if (accessor
== null)
457 accessor
= c
.GetRemoveMethod (true);
458 if (accessor
== null)
460 mattrs
= accessor
.Attributes
;
461 if ((mattrs
& MethodAttributes
.MemberAccessMask
) == MethodAttributes
.Public
) {
462 if ((bf
& BindingFlags
.Public
) != 0)
465 if ((bf
& BindingFlags
.NonPublic
) != 0)
471 if ((mattrs
& MethodAttributes
.Static
) != 0) {
472 if ((bf
& BindingFlags
.Static
) != 0)
475 if ((bf
& BindingFlags
.Instance
) != 0)
482 EventInfo
[] result
= new EventInfo
[l
.Count
];
487 public override Type
[] GetNestedTypes (BindingFlags bf
)
489 return generic_type
.GetNestedTypes (bf
);