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.Collections;
13 using System.Runtime.CompilerServices;
14 using System.Globalization;
15 using System.Runtime.Serialization;
17 namespace System.Reflection
19 internal class MonoGenericInst : MonoType
22 protected MonoGenericInst parent;
23 protected Type generic_type;
24 private MonoGenericInst[] interfaces;
25 private MethodInfo[] methods;
26 private ConstructorInfo[] ctors;
27 private FieldInfo[] fields;
30 internal MonoGenericInst ()
33 // this should not be used
34 throw new InvalidOperationException ();
37 [MethodImplAttribute(MethodImplOptions.InternalCall)]
38 private static extern MethodInfo inflate_method (MonoGenericInst declaring, MonoGenericInst reflected, MethodInfo method);
40 [MethodImplAttribute(MethodImplOptions.InternalCall)]
41 private static extern ConstructorInfo inflate_ctor (MonoGenericInst declaring, MonoGenericInst reflected, ConstructorInfo ctor);
43 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44 private static extern FieldInfo inflate_field (MonoGenericInst declaring, MonoGenericInst reflected, FieldInfo field);
46 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
47 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
49 protected void inflate (MonoGenericInst reflected,
50 ArrayList mlist, ArrayList clist, ArrayList flist)
53 parent.inflate (parent, mlist, clist, flist);
54 else if (BaseType != null) {
55 mlist.AddRange (generic_type.BaseType.GetMethods (flags));
56 clist.AddRange (generic_type.BaseType.GetConstructors (flags));
57 flist.AddRange (generic_type.BaseType.GetFields (flags));
58 } else if (interfaces != null) {
59 foreach (MonoGenericInst iface in interfaces)
60 iface.inflate (iface, mlist, clist, flist);
63 foreach (MethodInfo m in generic_type.GetMethods (flags))
64 mlist.Add (inflate_method (this, reflected, m));
65 foreach (ConstructorInfo c in generic_type.GetConstructors (flags))
66 clist.Add (inflate_ctor (this, reflected, c));
67 foreach (FieldInfo f in generic_type.GetFields (flags))
68 flist.Add (inflate_field (this, reflected, f));
73 ArrayList mlist = new ArrayList ();
74 ArrayList clist = new ArrayList ();
75 ArrayList flist = new ArrayList ();
77 inflate (this, mlist, clist, flist);
79 methods = new MethodInfo [mlist.Count];
80 mlist.CopyTo (methods, 0);
82 ctors = new ConstructorInfo [clist.Count];
83 clist.CopyTo (ctors, 0);
85 fields = new FieldInfo [flist.Count];
86 flist.CopyTo (fields, 0);
89 public override Type BaseType {
90 get { return parent != null ? parent : generic_type.BaseType; }
93 protected override bool IsValueTypeImpl ()
97 if (BaseType == typeof (Enum) || BaseType == typeof (ValueType))
100 return BaseType.IsSubclassOf (typeof (ValueType));
103 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
108 return GetMethods_impl (bindingAttr);
111 protected MethodInfo[] GetMethods_impl (BindingFlags bindingAttr)
113 ArrayList l = new ArrayList ();
115 MethodAttributes mattrs;
117 foreach (MethodInfo c in methods) {
119 mattrs = c.Attributes;
120 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
121 if ((bindingAttr & BindingFlags.Public) != 0)
124 if ((bindingAttr & BindingFlags.NonPublic) != 0)
130 if ((mattrs & MethodAttributes.Static) != 0) {
131 if ((bindingAttr & BindingFlags.Static) != 0)
134 if ((bindingAttr & BindingFlags.Instance) != 0)
141 MethodInfo[] result = new MethodInfo [l.Count];
146 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
151 return GetConstructors_impl (bindingAttr);
154 protected ConstructorInfo[] GetConstructors_impl (BindingFlags bindingAttr)
156 ArrayList l = new ArrayList ();
158 MethodAttributes mattrs;
160 foreach (ConstructorInfo c in ctors) {
162 mattrs = c.Attributes;
163 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
164 if ((bindingAttr & BindingFlags.Public) != 0)
167 if ((bindingAttr & BindingFlags.NonPublic) != 0)
173 if ((mattrs & MethodAttributes.Static) != 0) {
174 if ((bindingAttr & BindingFlags.Static) != 0)
177 if ((bindingAttr & BindingFlags.Instance) != 0)
184 ConstructorInfo[] result = new ConstructorInfo [l.Count];
189 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
194 return GetFields_impl (bindingAttr);
197 protected FieldInfo[] GetFields_impl (BindingFlags bindingAttr)
199 ArrayList l = new ArrayList ();
201 FieldAttributes fattrs;
203 foreach (FieldInfo c in fields) {
205 fattrs = c.Attributes;
206 if ((fattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
207 if ((bindingAttr & BindingFlags.Public) != 0)
210 if ((bindingAttr & BindingFlags.NonPublic) != 0)
216 if ((fattrs & FieldAttributes.Static) != 0) {
217 if ((bindingAttr & BindingFlags.Static) != 0)
220 if ((bindingAttr & BindingFlags.Instance) != 0)
227 FieldInfo[] result = new FieldInfo [l.Count];
233 internal class MonoInflatedMethod : MonoMethod
235 private readonly MethodInfo declaring;
236 private readonly MonoGenericInst declaring_type;
237 private readonly MonoGenericInst reflected_type;
238 private readonly IntPtr ginst;
240 public override Type DeclaringType {
242 return declaring_type != null ? declaring_type : base.DeclaringType;
246 public override Type ReflectedType {
248 return reflected_type != null ? reflected_type : base.ReflectedType;
252 public override bool IsDefined (Type attributeType, bool inherit)
257 public override object[] GetCustomAttributes (bool inherit)
260 return new object [0];
262 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
265 return new object [0];
269 internal class MonoInflatedCtor : MonoCMethod
271 private readonly ConstructorInfo declaring;
272 private readonly MonoGenericInst declaring_type;
273 private readonly MonoGenericInst reflected_type;
274 private readonly IntPtr ginst;
276 public override Type DeclaringType {
278 return declaring_type != null ? declaring_type : base.DeclaringType;
282 public override Type ReflectedType {
284 return reflected_type != null ? reflected_type : base.ReflectedType;
288 public override bool IsDefined (Type attributeType, bool inherit)
293 public override object[] GetCustomAttributes (bool inherit)
296 return new object [0];
298 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
301 return new object [0];
305 internal class MonoInflatedField : MonoField
307 private readonly IntPtr dhandle;
308 private readonly MonoGenericInst declaring_type;
309 private readonly MonoGenericInst reflected_type;
311 public override Type DeclaringType {
313 return declaring_type != null ? declaring_type : base.DeclaringType;
317 public override Type ReflectedType {
319 return reflected_type != null ? reflected_type : base.ReflectedType;
324 internal class MonoGenericParam : MonoType
326 private object refobj;
330 private Type[] constraints;
334 internal MonoGenericParam ()
337 // this should not be used
338 throw new InvalidOperationException ();
341 [MethodImplAttribute(MethodImplOptions.InternalCall)]
342 private extern void initialize ();
344 public void SetConstraints (Type[] constraints)
346 this.constraints = constraints;
350 public override Type BaseType {
353 throw new InvalidOperationException ();
354 if ((constraints.Length == 0) || constraints [0].IsInterface)
357 return constraints [0];
361 public override Type[] GetInterfaces ()
364 throw new InvalidOperationException ();
366 if ((constraints.Length == 0) || constraints [0].IsInterface)
369 Type[] ret = new Type [constraints.Length-1];
370 Array.Copy (constraints, 1, ret, 0, constraints.Length-1);