svn path=/trunk/mcs/; revision=104772
[mono.git] / mcs / class / corlib / System / MonoType.cs
1 //
2 // System.MonoType
3 //
4 // Authors: 
5 //      Sean MacIsaac (macisaac@ximian.com)
6 //      Paolo Molaro (lupus@ximian.com)
7 //      Patrik Torstensson (patrik.torstensson@labs2.com)
8 //      Gonzalo Paniagua (gonzalo@ximian.com)
9 //
10 // (c) 2001-2003 Ximian, Inc.
11 // Copyright (C) 2003-2005 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.Serialization;
37 using System.Security;
38
39 namespace System
40 {
41         [Serializable]
42         internal class MonoType : Type, ISerializable
43         {
44
45                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46                 private static extern void type_from_obj (MonoType type, Object obj);
47                 
48                 internal MonoType (Object obj)
49                 {
50                         // this should not be used - lupus
51                         type_from_obj (this, obj);
52                         
53                         throw new NotImplementedException ();
54                 }
55
56                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57                 private static extern TypeAttributes get_attributes (Type type);
58         
59                 protected override TypeAttributes GetAttributeFlagsImpl ()
60                 {
61                         return get_attributes (this);
62                 }
63
64                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
65                                                                        Binder binder,
66                                                                        CallingConventions callConvention,
67                                                                        Type[] types,
68                                                                        ParameterModifier[] modifiers)
69                 {
70                         if (bindingAttr == BindingFlags.Default)
71                                 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
72
73                         ConstructorInfo[] methods = GetConstructors (bindingAttr);
74                         ConstructorInfo found = null;
75                         MethodBase[] match;
76                         int count = 0;
77                         foreach (ConstructorInfo m in methods) {
78                                 // Under MS.NET, Standard|HasThis matches Standard...
79                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
80                                         continue;
81                                 found = m;
82                                 count++;
83                         }
84                         if (count == 0)
85                                 return null;
86                         if (types == null) {
87                                 if (count > 1)
88                                         throw new AmbiguousMatchException ();
89                                 return (ConstructorInfo) CheckMethodSecurity (found);
90                         }
91                         match = new MethodBase [count];
92                         if (count == 1)
93                                 match [0] = found;
94                         else {
95                                 count = 0;
96                                 foreach (ConstructorInfo m in methods) {
97                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
98                                                 continue;
99                                         match [count++] = m;
100                                 }
101                         }
102                         if (binder == null)
103                                 binder = Binder.DefaultBinder;
104                         return (ConstructorInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
105                 }
106
107                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108                 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
109
110                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
111                 {
112                         return GetConstructors_internal (bindingAttr, this);
113                 }
114
115                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
116                 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
117
118                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
119                 {
120                         if (name == null)
121                                 throw new ArgumentNullException ("name");
122
123                         return InternalGetEvent (name, bindingAttr);
124                 }
125
126                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
127                 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
128
129                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
130                 {
131                         return GetEvents_internal (bindingAttr, this);
132                 }
133
134                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
135                 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
136
137                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
138                 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
139
140                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
141                 {
142                         return GetFields_internal (bindingAttr, this);
143                 }
144                 
145                 public override Type GetInterface (string name, bool ignoreCase)
146                 {
147                         if (name == null)
148                                 throw new ArgumentNullException ();
149
150                         Type[] interfaces = GetInterfaces();
151
152                         foreach (Type type in interfaces) {
153                                 if (String.Compare (type.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
154                                         return type;
155                                 if (String.Compare (type.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
156                                         return type;
157                         }
158
159                         return null;
160                 }
161
162                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
163                 public extern override Type[] GetInterfaces();
164                 
165                 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
166                 {
167                         return FindMembers (MemberTypes.All, bindingAttr, null, null);
168                 }
169
170                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
171                 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
172
173                 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
174                 {
175                         return GetMethodsByName (null, bindingAttr, false, this);
176                 }
177
178                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
179                                                              Binder binder,
180                                                              CallingConventions callConvention,
181                                                              Type[] types, ParameterModifier[] modifiers)
182                 {
183                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
184                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
185                         MethodInfo found = null;
186                         MethodBase[] match;
187                         int count = 0;
188                         
189                         foreach (MethodInfo m in methods) {
190                                 // Under MS.NET, Standard|HasThis matches Standard...
191                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
192                                         continue;
193                                 found = m;
194                                 count++;
195                         }
196
197                         if (count == 0)
198                                 return null;
199                         
200                         if (count == 1 && types == null) 
201                                 return (MethodInfo) CheckMethodSecurity (found);
202
203                         match = new MethodBase [count];
204                         if (count == 1)
205                                 match [0] = found;
206                         else {
207                                 count = 0;
208                                 foreach (MethodInfo m in methods) {
209                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
210                                                 continue;
211                                         match [count++] = m;
212                                 }
213                         }
214
215                         if (types == null) 
216                                 return (MethodInfo) CheckMethodSecurity (Binder.FindMostDerivedMatch (match));
217
218                         if (binder == null)
219                                 binder = Binder.DefaultBinder;
220                         
221                         return (MethodInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
222                 }
223
224                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
225                 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
226
227                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
228                 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
229
230                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
231                 {
232                         return GetCorrespondingInflatedMethod (fromNoninstanciated);
233                 }
234
235                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
236                 {
237                         return GetCorrespondingInflatedConstructor (fromNoninstanciated);
238                 }
239
240                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
241                 {
242                         /* create sensible flags from given FieldInfo */
243                         BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
244                         flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
245                         return GetField (fromNoninstanciated.Name, flags);
246                 }
247
248                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
249                 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
250
251                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
252                 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
253
254                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
255                 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
256
257                 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
258                 {
259                         return GetPropertiesByName (null, bindingAttr, false, this);
260                 }
261
262                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
263                                                                  Binder binder, Type returnType,
264                                                                  Type[] types,
265                                                                  ParameterModifier[] modifiers)
266                 {
267                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
268                         PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
269                         int count = props.Length;
270                         if (count == 0)
271                                 return null;
272                         
273                         if (count == 1 && (types == null || types.Length == 0) && 
274                             (returnType == null || returnType == props[0].PropertyType))
275                                 return props [0];
276
277                         if (binder == null)
278                                 binder = Binder.DefaultBinder;
279
280                         return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
281                 }
282
283                 protected override bool HasElementTypeImpl ()
284                 {
285                         return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
286                 }
287
288                 protected override bool IsArrayImpl ()
289                 {
290                         return Type.IsArrayImpl (this);
291                 }
292
293                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
294                 protected extern override bool IsByRefImpl ();
295
296                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
297                 protected extern override bool IsCOMObjectImpl ();
298
299                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
300                 protected extern override bool IsPointerImpl ();
301
302                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
303                 protected extern override bool IsPrimitiveImpl ();
304
305                 public override bool IsSubclassOf (Type type)
306                 {
307                         if (type == null)
308                                 throw new ArgumentNullException ("type");
309
310                         return base.IsSubclassOf (type);
311                 }
312
313                 public override object InvokeMember (string name, BindingFlags invokeAttr,
314                                                      Binder binder, object target, object[] args,
315                                                      ParameterModifier[] modifiers,
316                                                      CultureInfo culture, string[] namedParameters)
317                 {
318 #if NET_2_0
319                         const string bindingflags_arg = "bindingFlags";
320 #else
321                         const string bindingflags_arg = "invokeAttr";
322 #endif
323
324
325                         if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
326                                 if ((invokeAttr & (BindingFlags.GetField |
327                                                 BindingFlags.GetField | BindingFlags.GetProperty |
328                                                 BindingFlags.SetProperty)) != 0)
329                                         throw new ArgumentException (bindingflags_arg);
330                         } else if (name == null)
331                                 throw new ArgumentNullException ("name");
332                         if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
333                                 throw new ArgumentException ("Cannot specify both Get and Set on a field.", bindingflags_arg);
334                         if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
335                                 throw new ArgumentException ("Cannot specify both Get and Set on a property.", bindingflags_arg);
336                         if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
337                                 if ((invokeAttr & BindingFlags.SetField) != 0)
338                                         throw new ArgumentException ("Cannot specify Set on a field and Invoke on a method.", bindingflags_arg);
339                                 if ((invokeAttr & BindingFlags.SetProperty) != 0)
340                                         throw new ArgumentException ("Cannot specify Set on a property and Invoke on a method.", bindingflags_arg);
341                         }
342                         if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
343                                 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
344                         if ((invokeAttr & (BindingFlags.InvokeMethod|BindingFlags.CreateInstance|BindingFlags.GetField|BindingFlags.SetField|BindingFlags.GetProperty|BindingFlags.SetProperty)) == 0)
345                                 throw new ArgumentException ("Must specify binding flags describing the invoke operation required.", bindingflags_arg);
346
347                         /* set some defaults if none are provided :-( */
348                         if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
349                                 invokeAttr |= BindingFlags.Public;
350                         if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
351                                 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
352
353                         if (binder == null)
354                                 binder = Binder.DefaultBinder;
355                         if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
356                                 /* the name is ignored */
357                                 invokeAttr |= BindingFlags.DeclaredOnly;
358                                 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
359                                 object state = null;
360                                 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
361                                 if (ctor == null) {
362                                         if (this.IsValueType && args == null)
363                                                 return Activator.CreateInstanceInternal (this);
364                                         
365                                         throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
366                                 }
367                                 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
368                                 binder.ReorderArgumentArray (ref args, state);
369                                 return result;
370                         }
371                         if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
372                                 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
373                                 name = attr.MemberName;
374                         }
375                         bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
376                         string throwMissingMethodDescription = null;
377                         bool throwMissingFieldException = false;
378                         
379                         if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
380                                 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
381                                 object state = null;
382                                 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
383                                 if (m == null) {
384                                         if (methods.Length > 0)
385                                                 throwMissingMethodDescription = "The best match for method " + name + " has some invalid parameter.";
386                                         else
387                                                 throwMissingMethodDescription = "Cannot find method " + name + ".";
388                                 } else {
389                                         ParameterInfo[] parameters = m.GetParameters();
390                                         for (int i = 0; i < parameters.Length; ++i) {
391                                                 if (System.Reflection.Missing.Value == args [i] && (parameters [i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault)
392                                                         throw new ArgumentException ("Used Missing.Value for argument without default value", "parameters");
393                                         }
394                                         bool hasParamArray = parameters.Length > 0 ? Attribute.IsDefined (parameters [parameters.Length - 1], 
395                                                 typeof (ParamArrayAttribute)) : false;
396                                         if (hasParamArray)
397                                                 ReorderParamArrayArguments (ref args, m);
398                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
399                                         binder.ReorderArgumentArray (ref args, state);
400                                         return result;
401                                 }
402                         }
403                         if ((invokeAttr & BindingFlags.GetField) != 0) {
404                                 FieldInfo f = GetField (name, invokeAttr);
405                                 if (f != null) {
406                                         return f.GetValue (target);
407                                 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
408                                         throwMissingFieldException = true;
409                                 }
410                                 /* try GetProperty */
411                         } else if ((invokeAttr & BindingFlags.SetField) != 0) {
412                                 FieldInfo f = GetField (name, invokeAttr);
413                                 if (f != null) {
414 #if NET_2_0
415                                         if (args == null)
416                                                 throw new ArgumentNullException ("providedArgs");
417 #endif
418                                         if ((args == null) || args.Length != 1)
419                                                 throw new ArgumentException ("Only the field value can be specified to set a field value.", bindingflags_arg);
420                                         f.SetValue (target, args [0]);
421                                         return null;
422                                 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
423                                         throwMissingFieldException = true;
424                                 }
425                                 /* try SetProperty */
426                         }
427                         if ((invokeAttr & BindingFlags.GetProperty) != 0) {
428                                 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
429                                 object state = null;
430                                 int i, count = 0;
431                                 for (i = 0; i < properties.Length; ++i) {
432                                         if ((properties [i].GetGetMethod (true) != null))
433                                                 count++;
434                                 }
435                                 MethodBase[] smethods = new MethodBase [count];
436                                 count = 0;
437                                 for (i = 0; i < properties.Length; ++i) {
438                                         MethodBase mb = properties [i].GetGetMethod (true);
439                                         if (mb != null)
440                                                 smethods [count++] = mb;
441                                 }
442                                 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
443                                 if (m == null) {
444                                         throwMissingFieldException = true;
445                                 } else {
446                                         ParameterInfo[] parameters = m.GetParameters();
447                                         bool hasParamArray = parameters.Length > 0 ? Attribute.IsDefined (parameters [parameters.Length - 1], 
448                                                 typeof (ParamArrayAttribute)) : false;
449                                         if (hasParamArray)
450                                                 ReorderParamArrayArguments (ref args, m);
451                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
452                                         binder.ReorderArgumentArray (ref args, state);
453                                         return result;
454                                 }
455                         } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
456                                 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
457                                 object state = null;
458                                 int i, count = 0;
459                                 for (i = 0; i < properties.Length; ++i) {
460                                         if (properties [i].GetSetMethod (true) != null)
461                                                 count++;
462                                 }
463                                 MethodBase[] smethods = new MethodBase [count];
464                                 count = 0;
465                                 for (i = 0; i < properties.Length; ++i) {
466                                         MethodBase mb = properties [i].GetSetMethod (true);
467                                         if (mb != null)
468                                                 smethods [count++] = mb;
469                                 }
470                                 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
471                                 if (m == null) {
472                                         throwMissingFieldException = true;
473                                 } else {
474                                         ParameterInfo[] parameters = m.GetParameters();
475                                         bool hasParamArray = parameters.Length > 0 ? Attribute.IsDefined (parameters [parameters.Length - 1], 
476                                                 typeof (ParamArrayAttribute)) : false;
477                                         if (hasParamArray)
478                                                 ReorderParamArrayArguments (ref args, m);
479                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
480                                         binder.ReorderArgumentArray (ref args, state);
481                                         return result;
482                                 }
483                         }
484                         if (throwMissingMethodDescription != null)
485                                 throw new MissingMethodException(throwMissingMethodDescription);
486                         if (throwMissingFieldException)
487                                 throw new MissingFieldException("Cannot find variable " + name + ".");
488
489                         return null;
490                 }
491
492                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
493                 public extern override Type GetElementType ();
494
495                 public override Type UnderlyingSystemType {
496                         get {
497                                 // This has _nothing_ to do with getting the base type of an enum etc.
498                                 return this;
499                         }
500                 }
501
502                 public extern override Assembly Assembly {
503                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
504                         get;
505                 }
506
507                 public override string AssemblyQualifiedName {
508                         get {
509                                 return getFullName (true, true);
510                         }
511                 }
512
513                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
514                 private extern string getFullName(bool full_name, bool assembly_qualified);
515
516                 public extern override Type BaseType {
517                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
518                         get;
519                 }
520
521                 public override string FullName {
522                         get {
523                                 return getFullName (true, false);
524                         }
525                 }
526
527                 public override Guid GUID {
528                         get {
529                                 object[] att = GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
530                                 if (att.Length == 0)
531                                         return Guid.Empty;
532                                 return new Guid(((System.Runtime.InteropServices.GuidAttribute)att[0]).Value);
533                         }
534                 }
535
536                 public override bool IsDefined (Type attributeType, bool inherit)
537                 {
538                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
539                 }
540
541                 public override object[] GetCustomAttributes (bool inherit)
542                 {
543                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
544                 }
545
546                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
547                 {
548                         if (attributeType == null)
549                         {
550                                 throw new ArgumentNullException("attributeType");
551                         }
552                         
553                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
554                 }
555
556                 public override MemberTypes MemberType {
557                         get {
558                                 if (DeclaringType != null)
559                                         return MemberTypes.NestedType;
560                                 else
561                                         return MemberTypes.TypeInfo;
562                         }
563                 }
564
565                 public extern override string Name {
566                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
567                         get;
568                 }
569
570                 public extern override string Namespace {
571                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
572                         get;
573                 }
574
575                 public extern override Module Module {
576                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
577                         get;
578                 }
579
580                 public extern override Type DeclaringType {
581                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
582                         get;
583                 }
584
585                 public override Type ReflectedType {
586                         get {
587                                 return DeclaringType;
588                         }
589                 }
590
591                 public override RuntimeTypeHandle TypeHandle {
592                         get {
593                                 return _impl;
594                         }
595                 }
596
597                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
598                 public extern override int GetArrayRank ();
599
600                 public void GetObjectData(SerializationInfo info, StreamingContext context)
601                 {
602                         UnitySerializationHolder.GetTypeData (this, info, context);
603                 }
604
605                 public override string ToString()
606                 {
607                         return getFullName (false, false);
608                 }
609
610 #if NET_2_0 || BOOTSTRAP_NET_2_0
611                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
612                 public extern override Type [] GetGenericArguments ();
613
614                 public override bool ContainsGenericParameters {
615                         get {
616                                 if (IsGenericParameter)
617                                         return true;
618
619                                 if (IsGenericType) {
620                                         foreach (Type arg in GetGenericArguments ())
621                                                 if (arg.ContainsGenericParameters)
622                                                         return true;
623                                 }
624
625                                 if (HasElementType)
626                                         return GetElementType ().ContainsGenericParameters;
627
628                                 return false;
629                         }
630                 }
631
632                 public extern override bool IsGenericParameter {
633                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
634                         get;
635                 }
636
637                 public extern override MethodBase DeclaringMethod {
638                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
639                         get;
640                 }
641 #endif
642
643                 private MethodBase CheckMethodSecurity (MethodBase mb)
644                 {
645 #if NET_2_1
646                         return mb;
647 #else
648                         if (!SecurityManager.SecurityEnabled || (mb == null))
649                                 return mb;
650
651                         // Sadly we have no way to know which kind of security action this is
652                         // so we must do it the hard way. Actually this isn't so bad 
653                         // because we can skip the (mb.Attributes & MethodAttributes.HasSecurity)
654                         // icall required (and do it ourselves)
655
656                         // this (unlike the Invoke step) is _and stays_ a LinkDemand (caller)
657                         return SecurityManager.ReflectedLinkDemandQuery (mb) ? mb : null;
658 #endif
659                 }
660
661                 void ReorderParamArrayArguments(ref object[] args, MethodBase method)
662                 {
663                         ParameterInfo[] parameters = method.GetParameters();
664                         object[] newArgs = new object [parameters.Length];
665                         Array paramArray = Array.CreateInstance(parameters[parameters.Length - 1].ParameterType.GetElementType(), 
666                                 args.Length - (parameters.Length - 1));
667                         int paramArrayCount = 0;
668                         for (int i = 0; i < args.Length; i++) {
669                                 if (i < (parameters.Length - 1))
670                                         newArgs [i] = args [i];
671                                 else {
672                                         paramArray.SetValue (args [i], paramArrayCount);
673                                         paramArrayCount ++;
674                                 }
675                         }
676                         newArgs [parameters.Length - 1] = paramArray;
677                         args = newArgs;
678                 }
679         }
680 }