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 MonoGenericClass : MonoType
45 protected Type generic_type;
49 internal MonoGenericClass ()
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 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
62 [MethodImplAttribute(MethodImplOptions.InternalCall)]
63 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
65 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66 extern FieldInfo GetCorrespondingInflatedField (string generic);
68 [MethodImplAttribute(MethodImplOptions.InternalCall)]
69 protected extern MethodInfo[] GetMethods_internal (Type reflected_type);
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 protected extern ConstructorInfo[] GetConstructors_internal (Type reflected_type);
74 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75 protected extern FieldInfo[] GetFields_internal (Type reflected_type);
77 [MethodImplAttribute(MethodImplOptions.InternalCall)]
78 protected extern PropertyInfo[] GetProperties_internal (Type reflected_type);
80 [MethodImplAttribute(MethodImplOptions.InternalCall)]
81 protected extern EventInfo[] GetEvents_internal (Type reflected_type);
83 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
84 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
86 EventInfo[] get_event_info ()
88 if (generic_type is TypeBuilder)
89 return ((TypeBuilder) generic_type).GetEvents_internal (flags);
91 return generic_type.GetEvents (flags);
99 MonoGenericClass parent = GetParentType ();
101 parent.initialize ();
103 initialize (generic_type.GetMethods (flags),
104 generic_type.GetConstructors (flags),
105 generic_type.GetFields (flags),
106 generic_type.GetProperties (flags),
112 [MethodImplAttribute(MethodImplOptions.InternalCall)]
113 protected extern MonoGenericClass GetParentType ();
115 [MethodImplAttribute(MethodImplOptions.InternalCall)]
116 protected extern MonoGenericClass[] GetInterfaces_internal ();
118 public override Type BaseType {
120 MonoGenericClass parent = GetParentType ();
121 return parent != null ? parent : generic_type.BaseType;
125 public override Type[] GetInterfaces ()
127 return GetInterfaces_internal ();
130 protected override bool IsValueTypeImpl ()
132 return generic_type.IsValueType;
135 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
139 return GetCorrespondingInflatedMethod (fromNoninstanciated);
142 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
146 return GetCorrespondingInflatedConstructor (fromNoninstanciated);
149 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
153 return GetCorrespondingInflatedField (fromNoninstanciated.Name);
156 public override MethodInfo[] GetMethods (BindingFlags bf)
158 ArrayList l = new ArrayList ();
161 // Walk up our class hierarchy and retrieve methods from our
165 Type current_type = this;
167 MonoGenericClass gi = current_type as MonoGenericClass;
169 l.AddRange (gi.GetMethods_impl (bf, this));
170 else if (current_type is TypeBuilder)
171 l.AddRange (current_type.GetMethods (bf));
173 // If we encounter a `MonoType', its
174 // GetMethodsByName() will return all the methods
175 // from its parent type(s), so we can stop here.
176 MonoType mt = (MonoType) current_type;
177 l.AddRange (mt.GetMethodsByName (null, bf, false, this));
181 if ((bf & BindingFlags.DeclaredOnly) != 0)
183 current_type = current_type.BaseType;
184 } while (current_type != null);
186 MethodInfo[] result = new MethodInfo [l.Count];
191 protected MethodInfo[] GetMethods_impl (BindingFlags bf, Type reftype)
193 ArrayList l = new ArrayList ();
195 MethodAttributes mattrs;
199 MethodInfo[] methods = GetMethods_internal (reftype);
201 for (int i = 0; i < methods.Length; i++) {
202 MethodInfo c = methods [i];
205 mattrs = c.Attributes;
206 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
207 if ((bf & BindingFlags.Public) != 0)
210 if ((bf & BindingFlags.NonPublic) != 0)
216 if ((mattrs & MethodAttributes.Static) != 0) {
217 if ((bf & BindingFlags.Static) != 0)
220 if ((bf & BindingFlags.Instance) != 0)
227 MethodInfo[] result = new MethodInfo [l.Count];
232 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
234 ArrayList l = new ArrayList ();
236 Type current_type = this;
238 MonoGenericClass gi = current_type as MonoGenericClass;
240 l.AddRange (gi.GetConstructors_impl (bf, this));
241 else if (current_type is TypeBuilder)
242 l.AddRange (current_type.GetConstructors (bf));
244 MonoType mt = (MonoType) current_type;
245 l.AddRange (mt.GetConstructors_internal (bf, this));
249 if ((bf & BindingFlags.DeclaredOnly) != 0)
251 current_type = current_type.BaseType;
252 } while (current_type != null);
254 ConstructorInfo[] result = new ConstructorInfo [l.Count];
259 protected ConstructorInfo[] GetConstructors_impl (BindingFlags bf, Type reftype)
261 ArrayList l = new ArrayList ();
263 MethodAttributes mattrs;
267 ConstructorInfo[] ctors = GetConstructors_internal (reftype);
269 for (int i = 0; i < ctors.Length; i++) {
270 ConstructorInfo c = ctors [i];
273 mattrs = c.Attributes;
274 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
275 if ((bf & BindingFlags.Public) != 0)
278 if ((bf & BindingFlags.NonPublic) != 0)
284 if ((mattrs & MethodAttributes.Static) != 0) {
285 if ((bf & BindingFlags.Static) != 0)
288 if ((bf & BindingFlags.Instance) != 0)
296 ConstructorInfo[] result = new ConstructorInfo [l.Count];
301 public override FieldInfo[] GetFields (BindingFlags bf)
303 ArrayList l = new ArrayList ();
305 Type current_type = this;
307 MonoGenericClass gi = current_type as MonoGenericClass;
309 l.AddRange (gi.GetFields_impl (bf, this));
310 else if (current_type is TypeBuilder)
311 l.AddRange (current_type.GetFields (bf));
313 MonoType mt = (MonoType) current_type;
314 l.AddRange (mt.GetFields_internal (bf, this));
318 if ((bf & BindingFlags.DeclaredOnly) != 0)
320 current_type = current_type.BaseType;
321 } while (current_type != null);
323 FieldInfo[] result = new FieldInfo [l.Count];
328 protected FieldInfo[] GetFields_impl (BindingFlags bf, Type reftype)
330 ArrayList l = new ArrayList ();
332 FieldAttributes fattrs;
336 FieldInfo[] fields = GetFields_internal (reftype);
338 for (int i = 0; i < fields.Length; i++) {
339 FieldInfo c = fields [i];
342 fattrs = c.Attributes;
343 if ((fattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
344 if ((bf & BindingFlags.Public) != 0)
347 if ((bf & BindingFlags.NonPublic) != 0)
353 if ((fattrs & FieldAttributes.Static) != 0) {
354 if ((bf & BindingFlags.Static) != 0)
357 if ((bf & BindingFlags.Instance) != 0)
364 FieldInfo[] result = new FieldInfo [l.Count];
369 public override PropertyInfo[] GetProperties (BindingFlags bf)
371 ArrayList l = new ArrayList ();
373 Type current_type = this;
375 MonoGenericClass gi = current_type as MonoGenericClass;
377 l.AddRange (gi.GetProperties_impl (bf, this));
378 else if (current_type is TypeBuilder)
379 l.AddRange (current_type.GetProperties (bf));
381 MonoType mt = (MonoType) current_type;
382 l.AddRange (mt.GetPropertiesByName (null, bf, false, this));
386 if ((bf & BindingFlags.DeclaredOnly) != 0)
388 current_type = current_type.BaseType;
389 } while (current_type != null);
391 PropertyInfo[] result = new PropertyInfo [l.Count];
396 protected PropertyInfo[] GetProperties_impl (BindingFlags bf, Type reftype)
398 ArrayList l = new ArrayList ();
400 MethodAttributes mattrs;
405 PropertyInfo[] properties = GetProperties_internal (reftype);
407 for (int i = 0; i < properties.Length; i++) {
408 PropertyInfo c = properties [i];
411 accessor = c.GetGetMethod (true);
412 if (accessor == null)
413 accessor = c.GetSetMethod (true);
414 if (accessor == null)
416 mattrs = accessor.Attributes;
417 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
418 if ((bf & BindingFlags.Public) != 0)
421 if ((bf & BindingFlags.NonPublic) != 0)
427 if ((mattrs & MethodAttributes.Static) != 0) {
428 if ((bf & BindingFlags.Static) != 0)
431 if ((bf & BindingFlags.Instance) != 0)
438 PropertyInfo[] result = new PropertyInfo [l.Count];
443 public override EventInfo[] GetEvents (BindingFlags bf)
445 ArrayList l = new ArrayList ();
447 Type current_type = this;
449 MonoGenericClass gi = current_type as MonoGenericClass;
451 l.AddRange (gi.GetEvents_impl (bf, this));
452 else if (current_type is TypeBuilder)
453 l.AddRange (current_type.GetEvents (bf));
455 MonoType mt = (MonoType) current_type;
456 l.AddRange (mt.GetEvents (bf));
460 if ((bf & BindingFlags.DeclaredOnly) != 0)
462 current_type = current_type.BaseType;
463 } while (current_type != null);
465 EventInfo[] result = new EventInfo [l.Count];
470 protected EventInfo[] GetEvents_impl (BindingFlags bf, Type reftype)
472 ArrayList l = new ArrayList ();
474 MethodAttributes mattrs;
479 EventInfo[] events = GetEvents_internal (reftype);
481 for (int i = 0; i < events.Length; i++) {
482 EventInfo c = events [i];
485 accessor = c.GetAddMethod (true);
486 if (accessor == null)
487 accessor = c.GetRemoveMethod (true);
488 if (accessor == null)
490 mattrs = accessor.Attributes;
491 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
492 if ((bf & BindingFlags.Public) != 0)
495 if ((bf & BindingFlags.NonPublic) != 0)
501 if ((mattrs & MethodAttributes.Static) != 0) {
502 if ((bf & BindingFlags.Static) != 0)
505 if ((bf & BindingFlags.Instance) != 0)
512 EventInfo[] result = new EventInfo [l.Count];
517 public override Type[] GetNestedTypes (BindingFlags bf)
519 return generic_type.GetNestedTypes (bf);