4 // Sean MacIsaac (macisaac@ximian.com)
5 // Paolo Molaro (lupus@ximian.com)
6 // Patrik Torstensson (patrik.torstensson@labs2.com)
8 // (C) 2001 Ximian, Inc.
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Collections;
14 using System.Runtime.CompilerServices;
15 using System.Globalization;
16 using System.Runtime.Serialization;
18 namespace System.Reflection
20 internal class MonoGenericInst : MonoType
22 protected Type generic_type;
26 internal MonoGenericInst ()
29 // this should not be used
30 throw new InvalidOperationException ();
33 [MethodImplAttribute(MethodImplOptions.InternalCall)]
34 protected extern void initialize (MethodInfo[] methods, ConstructorInfo[] ctors, FieldInfo[] fields, PropertyInfo[] properties);
36 [MethodImplAttribute(MethodImplOptions.InternalCall)]
37 protected extern MethodInfo[] GetMethods_internal (Type reflected_type);
39 [MethodImplAttribute(MethodImplOptions.InternalCall)]
40 protected extern ConstructorInfo[] GetConstructors_internal (Type reflected_type);
42 [MethodImplAttribute(MethodImplOptions.InternalCall)]
43 protected extern FieldInfo[] GetFields_internal (Type reflected_type);
45 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46 protected extern PropertyInfo[] GetProperties_internal (Type reflected_type);
48 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49 protected extern Type[] GetNestedTypes_internal ();
51 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
52 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
59 MonoGenericInst parent = GetParentType ();
63 initialize (generic_type.GetMethods (flags),
64 generic_type.GetConstructors (flags),
65 generic_type.GetFields (flags),
66 generic_type.GetProperties (flags));
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 protected extern MonoGenericInst GetParentType ();
74 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75 protected extern MonoGenericInst[] GetInterfaces_internal ();
77 public override Type BaseType {
79 MonoGenericInst parent = GetParentType ();
80 return parent != null ? parent : generic_type.BaseType;
84 public override Type DeclaringType {
85 get { return generic_type.DeclaringType; }
88 public override Type[] GetInterfaces ()
90 return GetInterfaces_internal ();
93 protected override bool IsValueTypeImpl ()
95 return generic_type.IsValueType;
98 public override MethodInfo[] GetMethods (BindingFlags bf)
102 ArrayList l = new ArrayList ();
105 // Walk up our class hierarchy and retrieve methods from our
109 Type current_type = this;
111 MonoGenericInst gi = current_type as MonoGenericInst;
113 l.AddRange (gi.GetMethods_impl (bf, this));
114 else if (current_type is TypeBuilder)
115 l.AddRange (current_type.GetMethods (bf));
117 // If we encounter a `MonoType', its
118 // GetMethodsByName() will return all the methods
119 // from its parent type(s), so we can stop here.
120 MonoType mt = (MonoType) current_type;
121 l.AddRange (mt.GetMethodsByName (null, bf, false, this));
125 if ((bf & BindingFlags.DeclaredOnly) != 0)
127 current_type = current_type.BaseType;
128 } while (current_type != null);
130 MethodInfo[] result = new MethodInfo [l.Count];
135 protected MethodInfo[] GetMethods_impl (BindingFlags bf, Type reftype)
137 ArrayList l = new ArrayList ();
139 MethodAttributes mattrs;
141 MethodInfo[] methods = GetMethods_internal (reftype);
143 for (int i = 0; i < methods.Length; i++) {
144 MethodInfo c = methods [i];
147 mattrs = c.Attributes;
148 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
149 if ((bf & BindingFlags.Public) != 0)
152 if ((bf & BindingFlags.NonPublic) != 0)
158 if ((mattrs & MethodAttributes.Static) != 0) {
159 if ((bf & BindingFlags.Static) != 0)
162 if ((bf & BindingFlags.Instance) != 0)
169 MethodInfo[] result = new MethodInfo [l.Count];
174 public override ConstructorInfo[] GetConstructors (BindingFlags bf)
178 ArrayList l = new ArrayList ();
180 Type current_type = this;
182 MonoGenericInst gi = current_type as MonoGenericInst;
184 l.AddRange (gi.GetConstructors_impl (bf, this));
185 else if (current_type is TypeBuilder)
186 l.AddRange (current_type.GetConstructors (bf));
188 MonoType mt = (MonoType) current_type;
189 l.AddRange (mt.GetConstructors_internal (bf, this));
193 if ((bf & BindingFlags.DeclaredOnly) != 0)
195 current_type = current_type.BaseType;
196 } while (current_type != null);
198 ConstructorInfo[] result = new ConstructorInfo [l.Count];
203 protected ConstructorInfo[] GetConstructors_impl (BindingFlags bf, Type reftype)
205 ArrayList l = new ArrayList ();
207 MethodAttributes mattrs;
209 ConstructorInfo[] ctors = GetConstructors_internal (reftype);
211 for (int i = 0; i < ctors.Length; i++) {
212 ConstructorInfo c = ctors [i];
215 mattrs = c.Attributes;
216 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
217 if ((bf & BindingFlags.Public) != 0)
220 if ((bf & BindingFlags.NonPublic) != 0)
226 if ((mattrs & MethodAttributes.Static) != 0) {
227 if ((bf & BindingFlags.Static) != 0)
230 if ((bf & BindingFlags.Instance) != 0)
238 ConstructorInfo[] result = new ConstructorInfo [l.Count];
243 public override FieldInfo[] GetFields (BindingFlags bf)
247 ArrayList l = new ArrayList ();
249 Type current_type = this;
251 MonoGenericInst gi = current_type as MonoGenericInst;
253 l.AddRange (gi.GetFields_impl (bf, this));
254 else if (current_type is TypeBuilder)
255 l.AddRange (current_type.GetFields (bf));
257 MonoType mt = (MonoType) current_type;
258 l.AddRange (mt.GetFields_internal (bf, this));
262 if ((bf & BindingFlags.DeclaredOnly) != 0)
264 current_type = current_type.BaseType;
265 } while (current_type != null);
267 FieldInfo[] result = new FieldInfo [l.Count];
272 protected FieldInfo[] GetFields_impl (BindingFlags bf, Type reftype)
274 ArrayList l = new ArrayList ();
276 FieldAttributes fattrs;
278 FieldInfo[] fields = GetFields_internal (reftype);
280 for (int i = 0; i < fields.Length; i++) {
281 FieldInfo c = fields [i];
284 fattrs = c.Attributes;
285 if ((fattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
286 if ((bf & BindingFlags.Public) != 0)
289 if ((bf & BindingFlags.NonPublic) != 0)
295 if ((fattrs & FieldAttributes.Static) != 0) {
296 if ((bf & BindingFlags.Static) != 0)
299 if ((bf & BindingFlags.Instance) != 0)
306 FieldInfo[] result = new FieldInfo [l.Count];
311 public override PropertyInfo[] GetProperties (BindingFlags bf)
315 ArrayList l = new ArrayList ();
317 Type current_type = this;
319 MonoGenericInst gi = current_type as MonoGenericInst;
321 l.AddRange (gi.GetProperties_impl (bf, this));
322 else if (current_type is TypeBuilder)
323 l.AddRange (current_type.GetProperties (bf));
325 MonoType mt = (MonoType) current_type;
326 l.AddRange (mt.GetPropertiesByName (null, bf, false, this));
330 if ((bf & BindingFlags.DeclaredOnly) != 0)
332 current_type = current_type.BaseType;
333 } while (current_type != null);
335 PropertyInfo[] result = new PropertyInfo [l.Count];
340 protected PropertyInfo[] GetProperties_impl (BindingFlags bf, Type reftype)
342 ArrayList l = new ArrayList ();
344 MethodAttributes mattrs;
347 PropertyInfo[] properties = GetProperties_internal (reftype);
349 for (int i = 0; i < properties.Length; i++) {
350 PropertyInfo c = properties [i];
353 accessor = c.GetGetMethod (true);
354 if (accessor == null)
355 accessor = c.GetSetMethod (true);
356 if (accessor == null)
358 mattrs = accessor.Attributes;
359 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
360 if ((bf & BindingFlags.Public) != 0)
363 if ((bf & BindingFlags.NonPublic) != 0)
369 if ((mattrs & MethodAttributes.Static) != 0) {
370 if ((bf & BindingFlags.Static) != 0)
373 if ((bf & BindingFlags.Instance) != 0)
380 PropertyInfo[] result = new PropertyInfo [l.Count];
385 public override Type[] GetNestedTypes (BindingFlags bf)
389 ArrayList l = new ArrayList ();
391 Type current_type = this;
393 MonoGenericInst gi = current_type as MonoGenericInst;
395 l.AddRange (gi.GetNestedTypes_impl (bf));
396 else if (current_type is TypeBuilder)
397 l.AddRange (current_type.GetNestedTypes (bf));
399 MonoType mt = (MonoType) current_type;
400 l.AddRange (mt.GetNestedTypes (bf));
404 if ((bf & BindingFlags.DeclaredOnly) != 0)
406 current_type = current_type.BaseType;
407 } while (current_type != null);
409 Type[] result = new Type [l.Count];
414 protected Type[] GetNestedTypes_impl (BindingFlags bindingAttr) {
415 ArrayList l = new ArrayList ();
417 TypeAttributes tattrs;
419 Type[] subtypes = GetNestedTypes_internal ();
420 foreach (Type t in subtypes) {
422 tattrs = t.Attributes;
423 if ((tattrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
424 if ((bindingAttr & BindingFlags.Public) != 0)
427 if ((bindingAttr & BindingFlags.NonPublic) != 0)
434 Type[] result = new Type [l.Count];
440 internal class MonoGenericParam : MonoType
442 private object refobj;
446 private Type[] constraints;
450 internal MonoGenericParam ()
453 // this should not be used
454 throw new InvalidOperationException ();
457 [MethodImplAttribute(MethodImplOptions.InternalCall)]
458 private extern void initialize ();
460 public void SetConstraints (Type[] constraints)
462 this.constraints = constraints;
466 public override Type BaseType {
469 throw new InvalidOperationException ();
470 if ((constraints.Length == 0) || constraints [0].IsInterface)
473 return constraints [0];
477 public override Type[] GetInterfaces ()
480 throw new InvalidOperationException ();
482 if ((constraints.Length == 0) || constraints [0].IsInterface)
485 Type[] ret = new Type [constraints.Length-1];
486 Array.Copy (constraints, 1, ret, 0, constraints.Length-1);