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