New tests.
[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.Collections.Generic;
34 using System.Globalization;
35 using System.Reflection;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.Serialization;
38 using System.Security;
39
40 namespace System
41 {
42         // Contains information about the type which is expensive to compute
43         internal class MonoTypeInfo {
44                 public string full_name;
45                 public ConstructorInfo default_ctor;
46         }
47                 
48         [Serializable]
49         internal class MonoType : Type, ISerializable
50         {
51                 [NonSerialized]
52                 MonoTypeInfo type_info;
53
54                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55                 private static extern void type_from_obj (MonoType type, Object obj);
56                 
57                 internal MonoType (Object obj)
58                 {
59                         // this should not be used - lupus
60                         type_from_obj (this, obj);
61                         
62                         throw new NotImplementedException ();
63                 }
64
65                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66                 private static extern TypeAttributes get_attributes (Type type);
67
68                 internal ConstructorInfo GetDefaultConstructor () {
69                         ConstructorInfo ctor = null;
70                         
71                         if (type_info == null)
72                                 type_info = new MonoTypeInfo ();
73                         if ((ctor = type_info.default_ctor) == null) {
74                                 const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
75         
76                                 ctor = type_info.default_ctor = GetConstructor (flags,  null, CallingConventions.Any, Type.EmptyTypes, null);
77                         }
78
79                         return ctor;
80                 }
81
82                 protected override TypeAttributes GetAttributeFlagsImpl ()
83                 {
84                         return get_attributes (this);
85                 }
86
87                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
88                                                                        Binder binder,
89                                                                        CallingConventions callConvention,
90                                                                        Type[] types,
91                                                                        ParameterModifier[] modifiers)
92                 {
93                         ConstructorInfo[] methods = GetConstructors (bindingAttr);
94                         return GetConstructorImpl (methods, bindingAttr, binder, callConvention, types, modifiers);
95                 }
96
97                 internal static ConstructorInfo GetConstructorImpl (ConstructorInfo[] methods, BindingFlags bindingAttr,
98                                                                        Binder binder,
99                                                                        CallingConventions callConvention,
100                                                                        Type[] types,
101                                                                        ParameterModifier[] modifiers)
102                 {
103                         if (bindingAttr == BindingFlags.Default)
104                                 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
105
106                         ConstructorInfo found = null;
107                         MethodBase[] match;
108                         int count = 0;
109                         foreach (ConstructorInfo m in methods) {
110                                 // Under MS.NET, Standard|HasThis matches Standard...
111                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
112                                         continue;
113                                 found = m;
114                                 count++;
115                         }
116                         if (count == 0)
117                                 return null;
118                         if (types == null) {
119                                 if (count > 1)
120                                         throw new AmbiguousMatchException ();
121                                 return (ConstructorInfo) CheckMethodSecurity (found);
122                         }
123                         match = new MethodBase [count];
124                         if (count == 1)
125                                 match [0] = found;
126                         else {
127                                 count = 0;
128                                 foreach (ConstructorInfo m in methods) {
129                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
130                                                 continue;
131                                         match [count++] = m;
132                                 }
133                         }
134                         if (binder == null)
135                                 binder = Binder.DefaultBinder;
136                         return (ConstructorInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
137                 }
138
139                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
140                 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
141
142                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
143                 {
144                         return GetConstructors_internal (bindingAttr, this);
145                 }
146
147                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
148                 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
149
150                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
151                 {
152                         if (name == null)
153                                 throw new ArgumentNullException ("name");
154
155                         return InternalGetEvent (name, bindingAttr);
156                 }
157
158                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
159                 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
160
161                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
162                 {
163                         return GetEvents_internal (bindingAttr, this);
164                 }
165
166                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
167                 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
168
169                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
170                 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
171
172                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
173                 {
174                         return GetFields_internal (bindingAttr, this);
175                 }
176                 
177                 public override Type GetInterface (string name, bool ignoreCase)
178                 {
179                         if (name == null)
180                                 throw new ArgumentNullException ();
181
182                         Type[] interfaces = GetInterfaces();
183
184                         foreach (Type type in interfaces) {
185                                 /*We must compare against the generic type definition*/
186                                 Type t = type.IsGenericType ? type.GetGenericTypeDefinition () : type;
187
188                                 if (String.Compare (t.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
189                                         return type;
190                                 if (String.Compare (t.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
191                                         return type;
192                         }
193
194                         return null;
195                 }
196
197                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
198                 public extern override Type[] GetInterfaces();
199                 
200                 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
201                 {
202                         return FindMembers (MemberTypes.All, bindingAttr, null, null);
203                 }
204
205                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
206                 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
207
208                 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
209                 {
210                         return GetMethodsByName (null, bindingAttr, false, this);
211                 }
212
213                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
214                                                              Binder binder,
215                                                              CallingConventions callConvention,
216                                                              Type[] types, ParameterModifier[] modifiers)
217                 {
218                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
219                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
220                         MethodInfo found = null;
221                         MethodBase[] match;
222                         int count = 0;
223                         
224                         foreach (MethodInfo m in methods) {
225                                 // Under MS.NET, Standard|HasThis matches Standard...
226                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
227                                         continue;
228                                 found = m;
229                                 count++;
230                         }
231
232                         if (count == 0)
233                                 return null;
234                         
235                         if (count == 1 && types == null) 
236                                 return (MethodInfo) CheckMethodSecurity (found);
237
238                         match = new MethodBase [count];
239                         if (count == 1)
240                                 match [0] = found;
241                         else {
242                                 count = 0;
243                                 foreach (MethodInfo m in methods) {
244                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
245                                                 continue;
246                                         match [count++] = m;
247                                 }
248                         }
249
250                         if (types == null) 
251                                 return (MethodInfo) CheckMethodSecurity (Binder.FindMostDerivedMatch (match));
252
253                         if (binder == null)
254                                 binder = Binder.DefaultBinder;
255                         
256                         return (MethodInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
257                 }
258
259                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
260                 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
261
262                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
263                 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
264
265                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
266                 {
267                         if (fromNoninstanciated == null)
268                                 throw new ArgumentNullException ("fromNoninstanciated");
269                         return GetCorrespondingInflatedMethod (fromNoninstanciated);
270                 }
271
272                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
273                 {
274                         if (fromNoninstanciated == null)
275                                 throw new ArgumentNullException ("fromNoninstanciated");
276                         return GetCorrespondingInflatedConstructor (fromNoninstanciated);
277                 }
278
279                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
280                 {
281                         /* create sensible flags from given FieldInfo */
282                         BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
283                         flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
284                         return GetField (fromNoninstanciated.Name, flags);
285                 }
286
287                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
288                 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
289
290                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
291                 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
292
293                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
294                 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
295
296                 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
297                 {
298                         return GetPropertiesByName (null, bindingAttr, false, this);
299                 }
300
301                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
302                                                                  Binder binder, Type returnType,
303                                                                  Type[] types,
304                                                                  ParameterModifier[] modifiers)
305                 {
306                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
307                         PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
308                         int count = props.Length;
309                         if (count == 0)
310                                 return null;
311                         
312                         if (count == 1 && (types == null || types.Length == 0) && 
313                             (returnType == null || returnType == props[0].PropertyType))
314                                 return props [0];
315
316                         if (binder == null)
317                                 binder = Binder.DefaultBinder;
318
319                         return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
320                 }
321
322                 protected override bool HasElementTypeImpl ()
323                 {
324                         return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
325                 }
326
327                 protected override bool IsArrayImpl ()
328                 {
329                         return Type.IsArrayImpl (this);
330                 }
331
332                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
333                 protected extern override bool IsByRefImpl ();
334
335                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
336                 protected extern override bool IsCOMObjectImpl ();
337
338                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
339                 protected extern override bool IsPointerImpl ();
340
341                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
342                 protected extern override bool IsPrimitiveImpl ();
343
344                 public override bool IsSubclassOf (Type type)
345                 {
346                         if (type == null)
347                                 throw new ArgumentNullException ("type");
348
349                         return base.IsSubclassOf (type);
350                 }
351
352                 public override object InvokeMember (string name, BindingFlags invokeAttr,
353                                                      Binder binder, object target, object[] args,
354                                                      ParameterModifier[] modifiers,
355                                                      CultureInfo culture, string[] namedParameters)
356                 {
357                         const string bindingflags_arg = "bindingFlags";
358
359
360                         if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
361                                 if ((invokeAttr & (BindingFlags.GetField |
362                                                 BindingFlags.GetField | BindingFlags.GetProperty |
363                                                 BindingFlags.SetProperty)) != 0)
364                                         throw new ArgumentException (bindingflags_arg);
365                         } else if (name == null)
366                                 throw new ArgumentNullException ("name");
367                         if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
368                                 throw new ArgumentException ("Cannot specify both Get and Set on a field.", bindingflags_arg);
369                         if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
370                                 throw new ArgumentException ("Cannot specify both Get and Set on a property.", bindingflags_arg);
371                         if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
372                                 if ((invokeAttr & BindingFlags.SetField) != 0)
373                                         throw new ArgumentException ("Cannot specify Set on a field and Invoke on a method.", bindingflags_arg);
374                                 if ((invokeAttr & BindingFlags.SetProperty) != 0)
375                                         throw new ArgumentException ("Cannot specify Set on a property and Invoke on a method.", bindingflags_arg);
376                         }
377                         if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
378                                 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
379                         if ((invokeAttr & (BindingFlags.InvokeMethod|BindingFlags.CreateInstance|BindingFlags.GetField|BindingFlags.SetField|BindingFlags.GetProperty|BindingFlags.SetProperty)) == 0)
380                                 throw new ArgumentException ("Must specify binding flags describing the invoke operation required.", bindingflags_arg);
381
382                         /* set some defaults if none are provided :-( */
383                         if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
384                                 invokeAttr |= BindingFlags.Public;
385                         if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
386                                 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
387
388                         if (binder == null)
389                                 binder = Binder.DefaultBinder;
390                         if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
391                                 /* the name is ignored */
392                                 invokeAttr |= BindingFlags.DeclaredOnly;
393                                 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
394                                 object state = null;
395                                 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
396                                 if (ctor == null) {
397                                         if (this.IsValueType && args == null)
398                                                 return Activator.CreateInstanceInternal (this);
399                                         
400                                         throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
401                                 }
402                                 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
403                                 binder.ReorderArgumentArray (ref args, state);
404                                 return result;
405                         }
406                         if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
407                                 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
408                                 name = attr.MemberName;
409                         }
410                         bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
411                         string throwMissingMethodDescription = null;
412                         bool throwMissingFieldException = false;
413                         
414                         if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
415                                 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
416                                 object state = null;
417                                 if (args == null)
418                                         args = new object [0];
419                                 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
420                                 if (m == null) {
421                                         if (methods.Length > 0)
422                                                 throwMissingMethodDescription = "The best match for method " + name + " has some invalid parameter.";
423                                         else
424                                                 throwMissingMethodDescription = "Cannot find method " + name + ".";
425                                 } else {
426                                         ParameterInfo[] parameters = m.GetParameters();
427                                         for (int i = 0; i < parameters.Length; ++i) {
428                                                 if (System.Reflection.Missing.Value == args [i] && (parameters [i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault)
429                                                         throw new ArgumentException ("Used Missing.Value for argument without default value", "parameters");
430                                         }
431                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
432                                         binder.ReorderArgumentArray (ref args, state);
433                                         return result;
434                                 }
435                         }
436                         if ((invokeAttr & BindingFlags.GetField) != 0) {
437                                 FieldInfo f = GetField (name, invokeAttr);
438                                 if (f != null) {
439                                         return f.GetValue (target);
440                                 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
441                                         throwMissingFieldException = true;
442                                 }
443                                 /* try GetProperty */
444                         } else if ((invokeAttr & BindingFlags.SetField) != 0) {
445                                 FieldInfo f = GetField (name, invokeAttr);
446                                 if (f != null) {
447                                         if (args == null)
448                                                 throw new ArgumentNullException ("providedArgs");
449                                         if ((args == null) || args.Length != 1)
450                                                 throw new ArgumentException ("Only the field value can be specified to set a field value.", bindingflags_arg);
451                                         f.SetValue (target, args [0]);
452                                         return null;
453                                 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
454                                         throwMissingFieldException = true;
455                                 }
456                                 /* try SetProperty */
457                         }
458                         if ((invokeAttr & BindingFlags.GetProperty) != 0) {
459                                 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
460                                 object state = null;
461                                 int i, count = 0;
462                                 for (i = 0; i < properties.Length; ++i) {
463                                         if ((properties [i].GetGetMethod (true) != null))
464                                                 count++;
465                                 }
466                                 MethodBase[] smethods = new MethodBase [count];
467                                 count = 0;
468                                 for (i = 0; i < properties.Length; ++i) {
469                                         MethodBase mb = properties [i].GetGetMethod (true);
470                                         if (mb != null)
471                                                 smethods [count++] = mb;
472                                 }
473                                 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
474                                 if (m == null) {
475                                         throwMissingFieldException = true;
476                                 } else {
477                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
478                                         binder.ReorderArgumentArray (ref args, state);
479                                         return result;
480                                 }
481                         } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
482                                 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
483                                 object state = null;
484                                 int i, count = 0;
485                                 for (i = 0; i < properties.Length; ++i) {
486                                         if (properties [i].GetSetMethod (true) != null)
487                                                 count++;
488                                 }
489                                 MethodBase[] smethods = new MethodBase [count];
490                                 count = 0;
491                                 for (i = 0; i < properties.Length; ++i) {
492                                         MethodBase mb = properties [i].GetSetMethod (true);
493                                         if (mb != null)
494                                                 smethods [count++] = mb;
495                                 }
496                                 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
497                                 if (m == null) {
498                                         throwMissingFieldException = true;
499                                 } else {
500                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
501                                         binder.ReorderArgumentArray (ref args, state);
502                                         return result;
503                                 }
504                         }
505                         if (throwMissingMethodDescription != null)
506                                 throw new MissingMethodException(throwMissingMethodDescription);
507                         if (throwMissingFieldException)
508                                 throw new MissingFieldException("Cannot find variable " + name + ".");
509
510                         return null;
511                 }
512
513                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
514                 public extern override Type GetElementType ();
515
516                 public override Type UnderlyingSystemType {
517                         get {
518                                 // This has _nothing_ to do with getting the base type of an enum etc.
519                                 return this;
520                         }
521                 }
522
523                 public extern override Assembly Assembly {
524                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
525                         get;
526                 }
527
528                 public override string AssemblyQualifiedName {
529                         get {
530                                 return getFullName (true, true);
531                         }
532                 }
533
534                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
535                 private extern string getFullName(bool full_name, bool assembly_qualified);
536
537                 public extern override Type BaseType {
538                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
539                         get;
540                 }
541
542                 public override string FullName {
543                         get {
544                                 string fullName;
545                                 // This doesn't need locking
546                                 if (type_info == null)
547                                         type_info = new MonoTypeInfo ();
548                                 if ((fullName = type_info.full_name) == null)
549                                         fullName = type_info.full_name = getFullName (true, false);
550
551                                 return fullName;
552                         }
553                 }
554
555                 public override Guid GUID {
556                         get {
557                                 object[] att = GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
558                                 if (att.Length == 0)
559                                         return Guid.Empty;
560                                 return new Guid(((System.Runtime.InteropServices.GuidAttribute)att[0]).Value);
561                         }
562                 }
563
564                 public override bool IsDefined (Type attributeType, bool inherit)
565                 {
566                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
567                 }
568
569                 public override object[] GetCustomAttributes (bool inherit)
570                 {
571                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
572                 }
573
574                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
575                 {
576                         if (attributeType == null)
577                         {
578                                 throw new ArgumentNullException("attributeType");
579                         }
580                         
581                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
582                 }
583
584                 public override MemberTypes MemberType {
585                         get {
586                                 if (DeclaringType != null && !IsGenericParameter)
587                                         return MemberTypes.NestedType;
588                                 else
589                                         return MemberTypes.TypeInfo;
590                         }
591                 }
592
593                 public extern override string Name {
594                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
595                         get;
596                 }
597
598                 public extern override string Namespace {
599                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
600                         get;
601                 }
602
603                 public extern override Module Module {
604                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
605                         get;
606                 }
607
608                 public extern override Type DeclaringType {
609                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
610                         get;
611                 }
612
613                 public override Type ReflectedType {
614                         get {
615                                 return DeclaringType;
616                         }
617                 }
618
619                 public override RuntimeTypeHandle TypeHandle {
620                         get {
621                                 return _impl;
622                         }
623                 }
624
625                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
626                 public extern override int GetArrayRank ();
627
628                 public void GetObjectData(SerializationInfo info, StreamingContext context)
629                 {
630                         UnitySerializationHolder.GetTypeData (this, info, context);
631                 }
632
633                 public override string ToString()
634                 {
635                         return getFullName (false, false);
636                 }
637
638                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639                 public extern override Type [] GetGenericArguments ();
640
641                 public override bool ContainsGenericParameters {
642                         get {
643                                 if (IsGenericParameter)
644                                         return true;
645
646                                 if (IsGenericType) {
647                                         foreach (Type arg in GetGenericArguments ())
648                                                 if (arg.ContainsGenericParameters)
649                                                         return true;
650                                 }
651
652                                 if (HasElementType)
653                                         return GetElementType ().ContainsGenericParameters;
654
655                                 return false;
656                         }
657                 }
658
659                 public extern override bool IsGenericParameter {
660                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
661                         get;
662                 }
663
664                 public extern override MethodBase DeclaringMethod {
665                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
666                         get;
667                 }
668
669                 public override Type GetGenericTypeDefinition () {
670                         Type res = GetGenericTypeDefinition_impl ();
671                         if (res == null)
672                                 throw new InvalidOperationException ();
673
674                         return res;
675                 }
676
677 #if NET_4_0
678                 public override IList<CustomAttributeData> GetCustomAttributesData () {
679                         return CustomAttributeData.GetCustomAttributes (this);
680                 }
681
682
683                 public override Array GetEnumValues () {
684                         if (!IsEnum)
685                                 throw new ArgumentException ("Type is not an enumeration", "enumType");
686
687                         return Enum.GetValues (this);
688                 }
689 #endif
690
691                 static MethodBase CheckMethodSecurity (MethodBase mb)
692                 {
693 #if NET_2_1
694                         return mb;
695 #else
696                         if (!SecurityManager.SecurityEnabled || (mb == null))
697                                 return mb;
698
699                         // Sadly we have no way to know which kind of security action this is
700                         // so we must do it the hard way. Actually this isn't so bad 
701                         // because we can skip the (mb.Attributes & MethodAttributes.HasSecurity)
702                         // icall required (and do it ourselves)
703
704                         // this (unlike the Invoke step) is _and stays_ a LinkDemand (caller)
705                         return SecurityManager.ReflectedLinkDemandQuery (mb) ? mb : null;
706 #endif
707                 }
708
709 #if NET_4_0
710                 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
711                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
712                 public extern int get_core_clr_security_level ();
713
714                 public override bool IsSecurityTransparent
715                 {
716                         get { return get_core_clr_security_level () == 0; }
717                 }
718
719                 public override bool IsSecurityCritical
720                 {
721                         get { return get_core_clr_security_level () > 0; }
722                 }
723
724                 public override bool IsSecuritySafeCritical
725                 {
726                         get { return get_core_clr_security_level () == 1; }
727                 }
728 #endif
729
730         }
731 }