Merge pull request #3281 from lambdageek/dev/g-ptr-array
[mono.git] / mcs / class / corlib / ReferenceSources / RuntimeType.cs
1 //
2 // RuntimeType.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2015 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Reflection;
30 using System.Runtime.CompilerServices;
31 using System.Threading;
32 using System.Collections.Generic;
33 using System.Runtime.InteropServices;
34 using System.Globalization;
35 #if MONO_COM
36 using System.Reflection.Emit;
37 #endif
38 using System.Diagnostics.Contracts;
39 using System.Security;
40 using System.Runtime.Serialization;
41
42 namespace System
43 {
44         // Contains information about the type which is expensive to compute
45         [StructLayout (LayoutKind.Sequential)]
46         internal class MonoTypeInfo {
47                 // this is the displayed form: special characters
48                 // ,+*&*[]\ in the identifier portions of the names
49                 // have been escaped with a leading backslash (\)
50                 public string full_name;
51                 public MonoCMethod default_ctor;
52         }
53
54         [StructLayout (LayoutKind.Sequential)]
55         partial class RuntimeType
56         {
57                 [NonSerialized]
58                 MonoTypeInfo type_info;
59
60                 internal Object GenericCache;
61
62                 internal RuntimeType (Object obj)
63                 {
64                         throw new NotImplementedException ();
65                 }
66
67                 internal MonoCMethod GetDefaultConstructor ()
68                 {
69                         MonoCMethod ctor = null;
70
71                         if (type_info == null)
72                                 type_info = new MonoTypeInfo ();
73                         else
74                                 ctor = type_info.default_ctor;
75
76                         if (ctor == null) {
77                                 var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
78
79                                 for (int i = 0; i < ctors.Length; ++i) {
80                                         if (ctors [i].GetParametersCount () == 0) {
81                                                 type_info.default_ctor = ctor = (MonoCMethod) ctors [i];
82                                                 break;
83                                         }
84                                 }
85                         }
86
87                         return ctor;
88                 }
89
90                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
91                 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
92
93                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94                 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
95
96                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
97                 {
98                         if (fromNoninstanciated == null)
99                                 throw new ArgumentNullException ("fromNoninstanciated");
100                         return GetCorrespondingInflatedMethod (fromNoninstanciated);
101                 }
102
103                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
104                 {
105                         if (fromNoninstanciated == null)
106                                 throw new ArgumentNullException ("fromNoninstanciated");
107                         return GetCorrespondingInflatedConstructor (fromNoninstanciated);
108                 }
109
110                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
111                 {
112                         /* create sensible flags from given FieldInfo */
113                         BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
114                         flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
115                         return GetField (fromNoninstanciated.Name, flags);
116                 }
117
118                 string GetDefaultMemberName ()
119                 {
120                         object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
121                         return att.Length != 0 ? ((DefaultMemberAttribute) att [0]).MemberName : null;
122                 }
123
124                 RuntimeConstructorInfo m_serializationCtor;
125                 internal RuntimeConstructorInfo GetSerializationCtor()
126                 {
127                         if (m_serializationCtor == null) {
128                                 var s_SICtorParamTypes = new Type[] { typeof(SerializationInfo), typeof(StreamingContext) };
129
130                                 m_serializationCtor = GetConstructor(
131                                         BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
132                                         null,
133                                         CallingConventions.Any,
134                                         s_SICtorParamTypes,
135                                         null) as RuntimeConstructorInfo;
136                         }
137
138                         return m_serializationCtor;
139                 }
140
141                 internal Object CreateInstanceSlow(bool publicOnly, bool skipCheckThis, bool fillCache, ref StackCrawlMark stackMark)
142                 {
143                         bool bNeedSecurityCheck = true;
144                         bool bCanBeCached = false;
145                         bool bSecurityCheckOff = false;
146
147                         if (!skipCheckThis)
148                                 CreateInstanceCheckThis();
149
150                         if (!fillCache)
151                                 bSecurityCheckOff = true;
152
153                         return CreateInstanceMono (!publicOnly);
154                 }
155
156                 object CreateInstanceMono (bool nonPublic)
157                 {
158                         var ctor = GetDefaultConstructor ();
159                         if (!nonPublic && ctor != null && !ctor.IsPublic) {
160                                 ctor = null;
161                         }
162
163                         if (ctor == null) {
164                                 Type elementType = this.GetRootElementType();
165                                 if (ReferenceEquals (elementType, typeof (TypedReference)) || ReferenceEquals (elementType, typeof (RuntimeArgumentHandle)))
166                                         throw new NotSupportedException (Environment.GetResourceString ("NotSupported_ContainsStackPtr"));
167
168                                 if (IsValueType)
169                                         return CreateInstanceInternal (this);
170
171                                 throw new MissingMethodException (Locale.GetText ("Default constructor not found for type " + FullName));
172                         }
173
174                         // TODO: .net does more checks in unmanaged land in RuntimeTypeHandle::CreateInstance
175                         if (IsAbstract) {
176                                 throw new MissingMethodException (Locale.GetText ("Cannot create an abstract class '{0}'.", FullName));
177                         }
178
179                         return ctor.InternalInvoke (null, null);
180                 }
181
182                 internal Object CheckValue (Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
183                 {
184                         bool failed = false;
185                         var res = TryConvertToType (value, ref failed);
186                         if (!failed)
187                                 return res;
188
189                         if ((invokeAttr & BindingFlags.ExactBinding) == BindingFlags.ExactBinding)
190                                 throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_ObjObjEx"), value.GetType(), this));
191
192                         if (binder != null && binder != Type.DefaultBinder)
193                                 return binder.ChangeType (value, this, culture);
194
195                         throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_ObjObjEx"), value.GetType(), this));
196                 }
197
198                 object TryConvertToType (object value, ref bool failed)
199                 {
200                         if (IsInstanceOfType (value)) {
201                                 return value;
202                         }
203
204                         if (IsByRef) {
205                                 var elementType = GetElementType ();
206                                 if (value == null || elementType.IsInstanceOfType (value)) {
207                                         return value;
208                                 }
209                         }
210
211                         if (value == null)
212                                 return value;
213
214                         if (IsEnum) {
215                                 var type = Enum.GetUnderlyingType (this);
216                                 if (type == value.GetType ())
217                                         return value;
218                                 var res = IsConvertibleToPrimitiveType (value, this);
219                                 if (res != null)
220                                         return res;
221                         } else if (IsPrimitive) {
222                                 var res = IsConvertibleToPrimitiveType (value, this);
223                                 if (res != null)
224                                         return res;
225                         } else if (IsPointer) {
226                                 var vtype = value.GetType ();
227                                 if (vtype == typeof (IntPtr) || vtype == typeof (UIntPtr))
228                                         return value;
229                         }
230
231                         failed = true;
232                         return null;
233                 }
234
235                 // Binder uses some incompatible conversion rules. For example
236                 // int value cannot be used with decimal parameter but in other
237                 // ways it's more flexible than normal convertor, for example
238                 // long value can be used with int based enum
239                 static object IsConvertibleToPrimitiveType (object value, Type targetType)
240                 {
241                         var type = value.GetType ();
242                         if (type.IsEnum) {
243                                 type = Enum.GetUnderlyingType (type);
244                                 if (type == targetType)
245                                         return value;
246                         }
247
248                         var from = Type.GetTypeCode (type);
249                         var to = Type.GetTypeCode (targetType);
250
251                         switch (to) {
252                                 case TypeCode.Char:
253                                         switch (from) {
254                                                 case TypeCode.Byte:
255                                                         return (Char) (Byte) value;
256                                                 case TypeCode.UInt16:
257                                                         return value;
258                                         }
259                                         break;
260                                 case TypeCode.Int16:
261                                         switch (from) {
262                                                 case TypeCode.Byte:
263                                                         return (Int16) (Byte) value;
264                                                 case TypeCode.SByte:
265                                                         return (Int16) (SByte) value;
266                                         }
267                                         break;
268                                 case TypeCode.UInt16:
269                                         switch (from) {
270                                                 case TypeCode.Byte:
271                                                         return (UInt16) (Byte) value;
272                                                 case TypeCode.Char:
273                                                         return value;
274                                         }
275                                         break;
276                                 case TypeCode.Int32:
277                                         switch (from) {
278                                                 case TypeCode.Byte:
279                                                         return (Int32) (Byte) value;
280                                                 case TypeCode.SByte:
281                                                         return (Int32) (SByte) value;
282                                                 case TypeCode.Char:
283                                                         return (Int32) (Char) value;
284                                                 case TypeCode.Int16:
285                                                         return (Int32) (Int16) value;
286                                                 case TypeCode.UInt16:
287                                                         return (Int32) (UInt16) value;
288                                         }
289                                         break;
290                                 case TypeCode.UInt32:
291                                         switch (from) {
292                                                 case TypeCode.Byte:
293                                                         return (UInt32) (Byte) value;
294                                                 case TypeCode.Char:
295                                                         return (UInt32) (Char) value;
296                                                 case TypeCode.UInt16:
297                                                         return (UInt32) (UInt16) value;
298                                         }
299                                         break;
300                                 case TypeCode.Int64:
301                                         switch (from) {
302                                                 case TypeCode.Byte:
303                                                         return (Int64) (Byte) value;
304                                                 case TypeCode.SByte:
305                                                         return (Int64) (SByte) value;
306                                                 case TypeCode.Int16:
307                                                         return (Int64) (Int16) value;
308                                                 case TypeCode.Char:
309                                                         return (Int64) (Char) value;
310                                                 case TypeCode.UInt16:
311                                                         return (Int64) (UInt16) value;
312                                                 case TypeCode.Int32:
313                                                         return (Int64) (Int32) value;
314                                                 case TypeCode.UInt32:
315                                                         return (Int64) (UInt32) value;
316                                         }
317                                         break;
318                                 case TypeCode.UInt64:
319                                         switch (from) {
320                                                 case TypeCode.Byte:
321                                                         return (UInt64) (Byte) value;
322                                                 case TypeCode.Char:
323                                                         return (UInt64) (Char) value;
324                                                 case TypeCode.UInt16:
325                                                         return (UInt64) (UInt16) value;
326                                                 case TypeCode.UInt32:
327                                                         return (UInt64) (UInt32) value;
328                                         }
329                                         break;
330                                 case TypeCode.Single:
331                                         switch (from) {
332                                                 case TypeCode.Byte:
333                                                         return (Single) (Byte) value;
334                                                 case TypeCode.SByte:
335                                                         return (Single) (SByte) value;
336                                                 case TypeCode.Int16:
337                                                         return (Single) (Int16) value;
338                                                 case TypeCode.Char:
339                                                         return (Single) (Char) value;
340                                                 case TypeCode.UInt16:
341                                                         return (Single) (UInt16) value;
342                                                 case TypeCode.Int32:
343                                                         return (Single) (Int32) value;
344                                                 case TypeCode.UInt32:
345                                                         return (Single) (UInt32) value;
346                                                 case TypeCode.Int64:
347                                                         return (Single) (Int64) value;
348                                                 case TypeCode.UInt64:
349                                                         return (Single) (UInt64) value;
350                                         }
351                                         break;
352                                 case TypeCode.Double:
353                                         switch (from) {
354                                                 case TypeCode.Byte:
355                                                         return (Double) (Byte) value;
356                                                 case TypeCode.SByte:
357                                                         return (Double) (SByte) value;
358                                                 case TypeCode.Char:
359                                                         return (Double) (Char) value;
360                                                 case TypeCode.Int16:
361                                                         return (Double) (Int16) value;
362                                                 case TypeCode.UInt16:
363                                                         return (Double) (UInt16) value;
364                                                 case TypeCode.Int32:
365                                                         return (Double) (Int32) value;
366                                                 case TypeCode.UInt32:
367                                                         return (Double) (UInt32) value;
368                                                 case TypeCode.Int64:
369                                                         return (Double) (Int64) value;
370                                                 case TypeCode.UInt64:
371                                                         return (Double) (UInt64) value;
372                                                 case TypeCode.Single:
373                                                         return (Double) (Single) value;
374                                         }
375                                         break;
376                         }
377
378                         // Everything else is rejected
379                         return null;
380                 }
381
382                 string GetCachedName (TypeNameKind kind)
383                 {
384                         switch (kind) {
385                         case TypeNameKind.SerializationName:
386                                 return ToString ();
387                         default:
388                                 throw new NotImplementedException ();
389                         }
390                 }
391
392                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
393                 extern Type make_array_type (int rank);
394
395                 public override Type MakeArrayType ()
396                 {
397                         return make_array_type (0);
398                 }
399
400                 public override Type MakeArrayType (int rank)
401                 {
402                         if (rank < 1 || rank > 255)
403                                 throw new IndexOutOfRangeException ();
404                         return make_array_type (rank);
405                 }
406
407                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
408                 extern Type make_byref_type ();
409
410                 public override Type MakeByRefType ()
411                 {
412                         if (IsByRef)
413                                 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
414                         return make_byref_type ();
415                 }
416
417                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
418                 static extern Type MakePointerType (Type type);
419
420                 public override Type MakePointerType ()
421                 {
422                         return MakePointerType (this);
423                 }
424
425                 public override StructLayoutAttribute StructLayoutAttribute {
426                         get {
427                                 return StructLayoutAttribute.GetCustomAttribute (this);
428                         }
429                 }
430
431                 public override bool ContainsGenericParameters {
432                         get {
433                                 if (IsGenericParameter)
434                                         return true;
435
436                                 if (IsGenericType) {
437                                         foreach (Type arg in GetGenericArguments ())
438                                                 if (arg.ContainsGenericParameters)
439                                                         return true;
440                                 }
441
442                                 if (HasElementType)
443                                         return GetElementType ().ContainsGenericParameters;
444
445                                 return false;
446                         }
447                 }
448
449                 public override Type[] GetGenericParameterConstraints()
450                 {
451                         if (!IsGenericParameter)
452                                 throw new InvalidOperationException(Environment.GetResourceString("Arg_NotGenericParameter"));
453                         Contract.EndContractBlock();
454
455                         var paramInfo = new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this));
456                         Type[] constraints = paramInfo.Constraints;
457
458                         if (constraints == null)
459                                 constraints = EmptyArray<Type>.Value;
460
461                         return constraints;
462                 }
463
464                 internal static object CreateInstanceForAnotherGenericParameter (Type genericType, RuntimeType genericArgument)
465                 {
466                         var gt = (RuntimeType) MakeGenericType (genericType, new Type [] { genericArgument });
467                         var ctor = gt.GetDefaultConstructor ();
468                         return ctor.InternalInvoke (null, null);
469                 }
470
471                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
472                 static extern Type MakeGenericType (Type gt, Type [] types);
473
474                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
475                 internal extern IntPtr GetMethodsByName_native (string name, BindingFlags bindingAttr, bool ignoreCase);
476
477                 internal RuntimeMethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, RuntimeType reflectedType)
478                 {
479                         var refh = new RuntimeTypeHandle (reflectedType);
480                         using (var h = new Mono.SafeGPtrArrayHandle (GetMethodsByName_native (name, bindingAttr, ignoreCase), false)) {
481                                 var n = h.Length;
482                                 var a = new RuntimeMethodInfo [n];
483                                 for (int i = 0; i < n; i++) {
484                                         var mh = new RuntimeMethodHandle (h[i]);
485                                         a[i] = (RuntimeMethodInfo) MethodBase.GetMethodFromHandleNoGenericCheck (mh, refh);
486                                 }
487                                 return a;
488                         }
489                 }
490
491                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
492                 extern RuntimePropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);              
493
494                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
495                 extern IntPtr GetConstructors_native (BindingFlags bindingAttr);
496
497                 RuntimeConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, RuntimeType reflectedType)
498                 {
499                         var refh = new RuntimeTypeHandle (reflectedType);
500                         using (var h = new Mono.SafeGPtrArrayHandle (GetConstructors_native (bindingAttr), false)) {
501                                 var n = h.Length;
502                                 var a = new RuntimeConstructorInfo [n];
503                                 for (int i = 0; i < n; i++) {
504                                         var mh = new RuntimeMethodHandle (h[i]);
505                                         a[i] = (RuntimeConstructorInfo) MethodBase.GetMethodFromHandleNoGenericCheck (mh, refh);
506                                 }
507                                 return a;
508                         }
509                 }
510
511
512                 public override InterfaceMapping GetInterfaceMap (Type ifaceType)
513                 {
514                         if (IsGenericParameter)
515                                 throw new InvalidOperationException(Environment.GetResourceString("Arg_GenericParameter"));
516                 
517                         if ((object)ifaceType == null)
518                                 throw new ArgumentNullException("ifaceType");
519                         Contract.EndContractBlock();
520
521                         RuntimeType ifaceRtType = ifaceType as RuntimeType;
522
523                         if (ifaceRtType == null)
524                                 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "ifaceType");
525
526                         InterfaceMapping res;
527                         if (!ifaceType.IsInterface)
528                                 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "ifaceType");
529                         if (IsInterface)
530                                 throw new ArgumentException ("'this' type cannot be an interface itself");
531                         res.TargetType = this;
532                         res.InterfaceType = ifaceType;
533                         GetInterfaceMapData (this, ifaceType, out res.TargetMethods, out res.InterfaceMethods);
534                         if (res.TargetMethods == null)
535                                 throw new ArgumentException (Locale.GetText ("Interface not found"), "ifaceType");
536
537                         return res;
538                 }
539
540                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
541                 static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);                
542
543                 public override Guid GUID {
544                         get {
545                                 object[] att = GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
546                                 if (att.Length == 0)
547                                         return Guid.Empty;
548                                 return new Guid(((System.Runtime.InteropServices.GuidAttribute)att[0]).Value);
549                         }
550                 }
551
552                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
553                 internal extern void GetPacking (out int packing, out int size);
554
555 #if MONO_COM
556                 private static Dictionary<Guid, Type> clsid_types;
557                 private static AssemblyBuilder clsid_assemblybuilder;
558 #endif
559
560                 internal static Type GetTypeFromCLSIDImpl(Guid clsid, String server, bool throwOnError)
561                 {
562 #if MONO_COM
563                         Type result;
564
565                         if (clsid_types == null)
566                         {
567                                 Dictionary<Guid, Type> new_clsid_types = new Dictionary<Guid, Type> ();
568                                 Interlocked.CompareExchange<Dictionary<Guid, Type>>(
569                                         ref clsid_types, new_clsid_types, null);
570                         }
571
572                         lock (clsid_types) {
573                                 if (clsid_types.TryGetValue(clsid, out result))
574                                         return result;
575
576                                 if (clsid_assemblybuilder == null)
577                                 {
578                                         AssemblyName assemblyname = new AssemblyName ();
579                                         assemblyname.Name = "GetTypeFromCLSIDDummyAssembly";
580                                         clsid_assemblybuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (
581                                                 assemblyname, AssemblyBuilderAccess.Run);
582                                 }
583                                 ModuleBuilder modulebuilder = clsid_assemblybuilder.DefineDynamicModule (
584                                         clsid.ToString ());
585
586                                 TypeBuilder typebuilder = modulebuilder.DefineType ("System.__ComObject",
587                                         TypeAttributes.Public | TypeAttributes.Class, typeof(System.__ComObject));
588
589                                 Type[] guidattrtypes = new Type[] { typeof(string) };
590
591                                 CustomAttributeBuilder customattr = new CustomAttributeBuilder (
592                                         typeof(GuidAttribute).GetConstructor (guidattrtypes),
593                                         new object[] { clsid.ToString () });
594
595                                 typebuilder.SetCustomAttribute (customattr);
596
597                                 customattr = new CustomAttributeBuilder (
598                                         typeof(ComImportAttribute).GetConstructor (EmptyTypes),
599                                         new object[0] {});
600
601                                 typebuilder.SetCustomAttribute (customattr);
602
603                                 result = typebuilder.CreateType ();
604
605                                 clsid_types.Add(clsid, result);
606
607                                 return result;
608                         }
609 #else
610                         throw new NotImplementedException ("Unmanaged activation removed");
611 #endif
612                 }
613
614                 protected override TypeCode GetTypeCodeImpl ()
615                 {
616                         return GetTypeCodeImplInternal (this);
617                 }
618
619                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
620                 extern static TypeCode GetTypeCodeImplInternal (Type type);             
621
622                 internal static Type GetTypeFromProgIDImpl(String progID, String server, bool throwOnError)
623                 {
624                         throw new NotImplementedException ("Unmanaged activation is not supported");
625                 }
626
627                 public override string ToString()
628                 {
629                         return getFullName (false, false);
630                 }
631
632                 bool IsGenericCOMObjectImpl ()
633                 {
634                         return false;
635                 }
636
637                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
638                 static extern object CreateInstanceInternal (Type type);
639
640                 public extern override MethodBase DeclaringMethod {
641                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
642                         get;
643                 }               
644                 
645                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
646                 internal extern string getFullName(bool full_name, bool assembly_qualified);
647
648                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
649                 extern Type[] GetGenericArgumentsInternal (bool runtimeArray);
650
651                 GenericParameterAttributes GetGenericParameterAttributes () {
652                         return (new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this))).Attributes;
653                 }
654
655                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656                 extern int GetGenericParameterPosition ();
657
658                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659                 extern RuntimeEventInfo[] GetEvents_internal (string name, BindingFlags bindingAttr, Type reflected_type);
660
661                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
662                 extern IntPtr GetFields_native (string name, BindingFlags bindingAttr);
663
664                 RuntimeFieldInfo[] GetFields_internal (string name, BindingFlags bindingAttr, RuntimeType reflectedType)
665                 {
666                         var refh = new RuntimeTypeHandle (reflectedType);
667                         using (var h = new Mono.SafeGPtrArrayHandle (GetFields_native (name, bindingAttr), false)) {
668                                 int n = h.Length;
669                                 var a = new RuntimeFieldInfo[n];
670                                 for (int i = 0; i < n; i++) {
671                                         var fh = new RuntimeFieldHandle (h[i]);
672                                         a[i] = (RuntimeFieldInfo) FieldInfo.GetFieldFromHandle (fh, refh);
673                                 }
674                                 return a;
675                         }
676                 }
677
678                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
679                 public extern override Type[] GetInterfaces();
680
681                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
682                 extern RuntimeType[] GetNestedTypes_internal (string name, BindingFlags bindingAttr);           
683
684                 public override string AssemblyQualifiedName {
685                         get {
686                                 return getFullName (true, true);
687                         }
688                 }
689
690                 public extern override Type DeclaringType {
691                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
692                         get;
693                 }
694
695                 public extern override string Name {
696                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
697                         get;
698                 }
699
700                 public extern override string Namespace {
701                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
702                         get;
703                 }
704
705 #if MOBILE
706                 static int get_core_clr_security_level ()
707                 {
708                         return 1;
709                 }
710 #else
711                 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
712                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
713                 public extern int get_core_clr_security_level ();
714
715                 public override bool IsSecurityTransparent {
716                         get { return get_core_clr_security_level () == 0; }
717                 }
718
719                 public override bool IsSecurityCritical {
720                         get { return get_core_clr_security_level () > 0; }
721                 }
722
723                 public override bool IsSecuritySafeCritical {
724                         get { return get_core_clr_security_level () == 1; }
725                 }
726 #endif
727
728                 public override int GetHashCode()
729                 {
730                         Type t = UnderlyingSystemType;
731                         if (t != null && t != this)
732                                 return t.GetHashCode ();
733                         return (int)_impl.Value;
734                 }
735
736                 public override string FullName {
737                         get {
738                                 string fullName;
739                                 // This doesn't need locking
740                                 if (type_info == null)
741                                         type_info = new MonoTypeInfo ();
742                                 if ((fullName = type_info.full_name) == null)
743                                         fullName = type_info.full_name = getFullName (true, false);
744
745                                 return fullName;
746                         }
747                 }
748
749                 internal override bool IsUserType {
750                         get {
751                                 return false;
752                         }
753                 }
754         }
755 }