[System.Reflection] CoreFX import for MemberInfo (#9746)
[mono-project.git] / mcs / class / corlib / ReferenceSources / RuntimeType.cs
blob13d972709c00c8035622641b0b1a3406a015fa34
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
42 namespace System
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;
54 [StructLayout (LayoutKind.Sequential)]
55 partial class RuntimeType
57 [NonSerialized]
58 MonoTypeInfo type_info;
60 internal Object GenericCache;
62 internal RuntimeType (Object obj)
64 throw new NotImplementedException ();
67 internal MonoCMethod GetDefaultConstructor ()
69 MonoCMethod ctor = null;
71 if (type_info == null)
72 type_info = new MonoTypeInfo ();
73 else
74 ctor = type_info.default_ctor;
76 if (ctor == null) {
77 var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
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;
87 return ctor;
90 [MethodImplAttribute(MethodImplOptions.InternalCall)]
91 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
96 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
98 if (fromNoninstanciated == null)
99 throw new ArgumentNullException ("fromNoninstanciated");
100 return GetCorrespondingInflatedMethod (fromNoninstanciated);
103 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
105 if (fromNoninstanciated == null)
106 throw new ArgumentNullException ("fromNoninstanciated");
107 return GetCorrespondingInflatedConstructor (fromNoninstanciated);
110 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
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);
118 string GetDefaultMemberName ()
120 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
121 return att.Length != 0 ? ((DefaultMemberAttribute) att [0]).MemberName : null;
124 RuntimeConstructorInfo m_serializationCtor;
125 internal RuntimeConstructorInfo GetSerializationCtor()
127 if (m_serializationCtor == null) {
128 var s_SICtorParamTypes = new Type[] { typeof(SerializationInfo), typeof(StreamingContext) };
130 m_serializationCtor = GetConstructor(
131 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
132 null,
133 CallingConventions.Any,
134 s_SICtorParamTypes,
135 null) as RuntimeConstructorInfo;
138 return m_serializationCtor;
141 internal Object CreateInstanceSlow(bool publicOnly, bool wrapExceptions, bool skipCheckThis, bool fillCache, ref StackCrawlMark stackMark)
143 //bool bNeedSecurityCheck = true;
144 //bool bCanBeCached = false;
145 //bool bSecurityCheckOff = false;
147 if (!skipCheckThis)
148 CreateInstanceCheckThis();
150 //if (!fillCache)
151 // bSecurityCheckOff = true;
153 return CreateInstanceMono (!publicOnly, wrapExceptions);
156 object CreateInstanceMono (bool nonPublic, bool wrapExceptions)
158 var ctor = GetDefaultConstructor ();
159 if (!nonPublic && ctor != null && !ctor.IsPublic) {
160 ctor = null;
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"));
168 if (IsValueType)
169 return CreateInstanceInternal (this);
171 throw new MissingMethodException (Locale.GetText ("Default constructor not found for type " + FullName));
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));
179 return ctor.InternalInvoke (null, null, wrapExceptions);
182 internal Object CheckValue (Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
184 bool failed = false;
185 var res = TryConvertToType (value, ref failed);
186 if (!failed)
187 return res;
189 if ((invokeAttr & BindingFlags.ExactBinding) == BindingFlags.ExactBinding)
190 throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_ObjObjEx"), value.GetType(), this));
192 if (binder != null && binder != Type.DefaultBinder)
193 return binder.ChangeType (value, this, culture);
195 throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_ObjObjEx"), value.GetType(), this));
198 object TryConvertToType (object value, ref bool failed)
200 if (IsInstanceOfType (value)) {
201 return value;
204 if (IsByRef) {
205 var elementType = GetElementType ();
206 if (value == null || elementType.IsInstanceOfType (value)) {
207 return value;
211 if (value == null)
212 return value;
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;
231 failed = true;
232 return null;
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)
241 var type = value.GetType ();
242 if (type.IsEnum) {
243 type = Enum.GetUnderlyingType (type);
244 if (type == targetType)
245 return value;
248 var from = Type.GetTypeCode (type);
249 var to = Type.GetTypeCode (targetType);
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;
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;
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;
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;
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;
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;
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;
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;
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;
375 break;
378 // Everything else is rejected
379 return null;
382 string GetCachedName (TypeNameKind kind)
384 switch (kind) {
385 case TypeNameKind.SerializationName:
386 return ToString ();
387 default:
388 throw new NotImplementedException ();
392 [MethodImplAttribute(MethodImplOptions.InternalCall)]
393 extern Type make_array_type (int rank);
395 public override Type MakeArrayType ()
397 return make_array_type (0);
400 public override Type MakeArrayType (int rank)
402 if (rank < 1 || rank > 255)
403 throw new IndexOutOfRangeException ();
404 return make_array_type (rank);
407 [MethodImplAttribute(MethodImplOptions.InternalCall)]
408 extern Type make_byref_type ();
410 public override Type MakeByRefType ()
412 if (IsByRef)
413 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
414 return make_byref_type ();
417 [MethodImplAttribute(MethodImplOptions.InternalCall)]
418 static extern Type MakePointerType (Type type);
420 public override Type MakePointerType ()
422 return MakePointerType (this);
425 public override StructLayoutAttribute StructLayoutAttribute {
426 get {
427 return StructLayoutAttribute.GetCustomAttribute (this);
431 public override bool ContainsGenericParameters {
432 get {
433 if (IsGenericParameter)
434 return true;
436 if (IsGenericType) {
437 foreach (Type arg in GetGenericArguments ())
438 if (arg.ContainsGenericParameters)
439 return true;
442 if (HasElementType)
443 return GetElementType ().ContainsGenericParameters;
445 return false;
449 public override Type[] GetGenericParameterConstraints()
451 if (!IsGenericParameter)
452 throw new InvalidOperationException(Environment.GetResourceString("Arg_NotGenericParameter"));
453 Contract.EndContractBlock();
455 var paramInfo = new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this));
456 Type[] constraints = paramInfo.Constraints;
458 if (constraints == null)
459 constraints = EmptyArray<Type>.Value;
461 return constraints;
464 internal static object CreateInstanceForAnotherGenericParameter (Type genericType, RuntimeType genericArgument)
466 var gt = (RuntimeType) MakeGenericType (genericType, new Type [] { genericArgument });
467 var ctor = gt.GetDefaultConstructor ();
468 return ctor.InternalInvoke (null, null, wrapExceptions: true);
471 [MethodImplAttribute(MethodImplOptions.InternalCall)]
472 static extern Type MakeGenericType (Type gt, Type [] types);
474 [MethodImplAttribute(MethodImplOptions.InternalCall)]
475 internal extern IntPtr GetMethodsByName_native (IntPtr namePtr, BindingFlags bindingAttr, MemberListType listType);
477 internal RuntimeMethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
479 var refh = new RuntimeTypeHandle (reflectedType);
480 using (var namePtr = new Mono.SafeStringMarshal (name))
481 using (var h = new Mono.SafeGPtrArrayHandle (GetMethodsByName_native (namePtr.Value, bindingAttr, listType))) {
482 var n = h.Length;
483 var a = new RuntimeMethodInfo [n];
484 for (int i = 0; i < n; i++) {
485 var mh = new RuntimeMethodHandle (h[i]);
486 a[i] = (RuntimeMethodInfo) MethodBase.GetMethodFromHandleNoGenericCheck (mh, refh);
488 return a;
492 [MethodImplAttribute(MethodImplOptions.InternalCall)]
493 extern IntPtr GetPropertiesByName_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
495 [MethodImplAttribute(MethodImplOptions.InternalCall)]
496 extern IntPtr GetConstructors_native (BindingFlags bindingAttr);
498 RuntimeConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, RuntimeType reflectedType)
500 var refh = new RuntimeTypeHandle (reflectedType);
501 using (var h = new Mono.SafeGPtrArrayHandle (GetConstructors_native (bindingAttr))) {
502 var n = h.Length;
503 var a = new RuntimeConstructorInfo [n];
504 for (int i = 0; i < n; i++) {
505 var mh = new RuntimeMethodHandle (h[i]);
506 a[i] = (RuntimeConstructorInfo) MethodBase.GetMethodFromHandleNoGenericCheck (mh, refh);
508 return a;
512 RuntimePropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
514 var refh = new RuntimeTypeHandle (reflectedType);
515 using (var namePtr = new Mono.SafeStringMarshal (name))
516 using (var h = new Mono.SafeGPtrArrayHandle (GetPropertiesByName_native (namePtr.Value, bindingAttr, listType))) {
517 var n = h.Length;
518 var a = new RuntimePropertyInfo [n];
519 for (int i = 0; i < n; i++) {
520 var ph = new Mono.RuntimePropertyHandle (h[i]);
521 a[i] = (RuntimePropertyInfo) PropertyInfo.GetPropertyFromHandle (ph, refh);
523 return a;
527 public override InterfaceMapping GetInterfaceMap (Type ifaceType)
529 if (IsGenericParameter)
530 throw new InvalidOperationException(Environment.GetResourceString("Arg_GenericParameter"));
532 if ((object)ifaceType == null)
533 throw new ArgumentNullException("ifaceType");
534 Contract.EndContractBlock();
536 RuntimeType ifaceRtType = ifaceType as RuntimeType;
538 if (ifaceRtType == null)
539 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "ifaceType");
541 InterfaceMapping res;
542 if (!ifaceType.IsInterface)
543 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "ifaceType");
544 if (IsInterface)
545 throw new ArgumentException ("'this' type cannot be an interface itself");
546 res.TargetType = this;
547 res.InterfaceType = ifaceType;
548 GetInterfaceMapData (this, ifaceType, out res.TargetMethods, out res.InterfaceMethods);
549 if (res.TargetMethods == null)
550 throw new ArgumentException (Locale.GetText ("Interface not found"), "ifaceType");
552 return res;
555 [MethodImplAttribute(MethodImplOptions.InternalCall)]
556 static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
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);
567 [MethodImplAttribute(MethodImplOptions.InternalCall)]
568 internal extern void GetPacking (out int packing, out int size);
570 #if MONO_COM
571 private static Dictionary<Guid, Type> clsid_types;
572 private static AssemblyBuilder clsid_assemblybuilder;
573 #endif
575 internal static Type GetTypeFromCLSIDImpl(Guid clsid, String server, bool throwOnError)
577 #if MONO_COM
578 Type result;
580 if (clsid_types == null)
582 Dictionary<Guid, Type> new_clsid_types = new Dictionary<Guid, Type> ();
583 Interlocked.CompareExchange<Dictionary<Guid, Type>>(
584 ref clsid_types, new_clsid_types, null);
587 lock (clsid_types) {
588 if (clsid_types.TryGetValue(clsid, out result))
589 return result;
591 if (clsid_assemblybuilder == null)
593 AssemblyName assemblyname = new AssemblyName ();
594 assemblyname.Name = "GetTypeFromCLSIDDummyAssembly";
595 /* Dynamically created assembly is marked internal to corlib to allow
596 __ComObject access for dynamic types. */
597 clsid_assemblybuilder = new AssemblyBuilder (assemblyname, null,
598 AssemblyBuilderAccess.Run, true);
600 ModuleBuilder modulebuilder = clsid_assemblybuilder.DefineDynamicModule (
601 clsid.ToString ());
603 TypeBuilder typebuilder = modulebuilder.DefineType ("System.__ComObject",
604 TypeAttributes.Public | TypeAttributes.Class, typeof(System.__ComObject));
606 Type[] guidattrtypes = new Type[] { typeof(string) };
608 CustomAttributeBuilder customattr = new CustomAttributeBuilder (
609 typeof(GuidAttribute).GetConstructor (guidattrtypes),
610 new object[] { clsid.ToString () });
612 typebuilder.SetCustomAttribute (customattr);
614 customattr = new CustomAttributeBuilder (
615 typeof(ComImportAttribute).GetConstructor (EmptyTypes),
616 new object[0] {});
618 typebuilder.SetCustomAttribute (customattr);
620 result = typebuilder.CreateType ();
622 clsid_types.Add(clsid, result);
624 return result;
626 #else
627 throw new NotImplementedException ("Unmanaged activation removed");
628 #endif
631 protected override TypeCode GetTypeCodeImpl ()
633 return GetTypeCodeImplInternal (this);
636 [MethodImplAttribute(MethodImplOptions.InternalCall)]
637 extern static TypeCode GetTypeCodeImplInternal (Type type);
639 internal static Type GetTypeFromProgIDImpl(String progID, String server, bool throwOnError)
641 throw new NotImplementedException ("Unmanaged activation is not supported");
644 public override string ToString()
646 return getFullName (false, false);
649 bool IsGenericCOMObjectImpl ()
651 return false;
654 [MethodImplAttribute (MethodImplOptions.InternalCall)]
655 static extern object CreateInstanceInternal (Type type);
657 public extern override MethodBase DeclaringMethod {
658 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659 get;
662 [MethodImplAttribute(MethodImplOptions.InternalCall)]
663 internal extern string getFullName(bool full_name, bool assembly_qualified);
665 [MethodImplAttribute(MethodImplOptions.InternalCall)]
666 extern Type[] GetGenericArgumentsInternal (bool runtimeArray);
668 GenericParameterAttributes GetGenericParameterAttributes () {
669 return (new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this))).Attributes;
672 [MethodImplAttribute(MethodImplOptions.InternalCall)]
673 extern int GetGenericParameterPosition ();
675 [MethodImplAttribute(MethodImplOptions.InternalCall)]
676 extern IntPtr GetEvents_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
678 [MethodImplAttribute(MethodImplOptions.InternalCall)]
679 extern IntPtr GetFields_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
681 RuntimeFieldInfo[] GetFields_internal (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
683 var refh = new RuntimeTypeHandle (reflectedType);
684 using (var namePtr = new Mono.SafeStringMarshal (name))
685 using (var h = new Mono.SafeGPtrArrayHandle (GetFields_native (namePtr.Value, bindingAttr, listType))) {
686 int n = h.Length;
687 var a = new RuntimeFieldInfo[n];
688 for (int i = 0; i < n; i++) {
689 var fh = new RuntimeFieldHandle (h[i]);
690 a[i] = (RuntimeFieldInfo) FieldInfo.GetFieldFromHandle (fh, refh);
692 return a;
696 RuntimeEventInfo[] GetEvents_internal (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
698 var refh = new RuntimeTypeHandle (reflectedType);
699 using (var namePtr = new Mono.SafeStringMarshal (name))
700 using (var h = new Mono.SafeGPtrArrayHandle (GetEvents_native (namePtr.Value, bindingAttr, listType))) {
701 int n = h.Length;
702 var a = new RuntimeEventInfo[n];
703 for (int i = 0; i < n; i++) {
704 var eh = new Mono.RuntimeEventHandle (h[i]);
705 a[i] = (RuntimeEventInfo) EventInfo.GetEventFromHandle (eh, refh);
707 return a;
711 [MethodImplAttribute(MethodImplOptions.InternalCall)]
712 public extern override Type[] GetInterfaces();
714 [MethodImplAttribute(MethodImplOptions.InternalCall)]
715 extern IntPtr GetNestedTypes_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
717 RuntimeType[] GetNestedTypes_internal (string displayName, BindingFlags bindingAttr, MemberListType listType)
719 string internalName = null;
720 if (displayName != null)
721 internalName = TypeIdentifiers.FromDisplay (displayName).InternalName;
722 using (var namePtr = new Mono.SafeStringMarshal (internalName))
723 using (var h = new Mono.SafeGPtrArrayHandle (GetNestedTypes_native (namePtr.Value, bindingAttr, listType))) {
724 int n = h.Length;
725 var a = new RuntimeType [n];
726 for (int i = 0; i < n; i++) {
727 var th = new RuntimeTypeHandle (h[i]);
728 a[i] = (RuntimeType) Type.GetTypeFromHandle (th);
730 return a;
734 public override string AssemblyQualifiedName {
735 get {
736 return getFullName (true, true);
740 public extern override Type DeclaringType {
741 [MethodImplAttribute(MethodImplOptions.InternalCall)]
742 get;
745 public extern override string Name {
746 [MethodImplAttribute(MethodImplOptions.InternalCall)]
747 get;
750 public extern override string Namespace {
751 [MethodImplAttribute(MethodImplOptions.InternalCall)]
752 get;
755 #if MOBILE
756 static int get_core_clr_security_level ()
758 return 1;
760 #else
761 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
762 [MethodImplAttribute(MethodImplOptions.InternalCall)]
763 public extern int get_core_clr_security_level ();
765 public override bool IsSecurityTransparent {
766 get { return get_core_clr_security_level () == 0; }
769 public override bool IsSecurityCritical {
770 get { return get_core_clr_security_level () > 0; }
773 public override bool IsSecuritySafeCritical {
774 get { return get_core_clr_security_level () == 1; }
776 #endif
778 public override int GetHashCode()
780 Type t = UnderlyingSystemType;
781 if (t != null && t != this)
782 return t.GetHashCode ();
783 return (int)_impl.Value;
786 public override string FullName {
787 get {
788 // https://bugzilla.xamarin.com/show_bug.cgi?id=57938
789 if (IsGenericType && ContainsGenericParameters && !IsGenericTypeDefinition)
790 return null;
792 string fullName;
793 // This doesn't need locking
794 if (type_info == null)
795 type_info = new MonoTypeInfo ();
796 if ((fullName = type_info.full_name) == null)
797 fullName = type_info.full_name = getFullName (true, false);
799 return fullName;
803 public sealed override bool HasSameMetadataDefinitionAs (MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeType> (other);
805 public override bool IsSZArray {
806 get {
807 // TODO: intrinsic
808 return IsArray && ReferenceEquals (this, GetElementType ().MakeArrayType ());
812 internal override bool IsUserType {
813 get {
814 return false;
818 [System.Runtime.InteropServices.ComVisible(true)]
819 [Pure]
820 public override bool IsSubclassOf(Type type)
822 if ((object)type == null)
823 throw new ArgumentNullException("type");
824 Contract.EndContractBlock();
825 RuntimeType rtType = type as RuntimeType;
826 if (rtType == null)
827 return false;
829 return RuntimeTypeHandle.IsSubclassOf (this, rtType);