[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / ReferenceSources / RuntimeType.cs
blob5adefd237a107f7a9ef7e97f46ce8379667d2181
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 RuntimeConstructorInfo default_ctor;
54 [StructLayout (LayoutKind.Sequential)]
55 partial class RuntimeType
57 #region keep in sync with object-internals.h
58 [NonSerialized]
59 MonoTypeInfo type_info;
60 #endregion
62 internal Object GenericCache;
64 internal RuntimeType (Object obj)
66 throw new NotImplementedException ();
69 internal RuntimeConstructorInfo GetDefaultConstructor ()
71 RuntimeConstructorInfo ctor = null;
73 if (type_info == null)
74 type_info = new MonoTypeInfo ();
75 else
76 ctor = type_info.default_ctor;
78 if (ctor == null) {
79 var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
81 for (int i = 0; i < ctors.Length; ++i) {
82 if (ctors [i].GetParametersCount () == 0) {
83 type_info.default_ctor = ctor = (RuntimeConstructorInfo) ctors [i];
84 break;
89 return ctor;
92 [MethodImplAttribute(MethodImplOptions.InternalCall)]
93 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
95 [MethodImplAttribute(MethodImplOptions.InternalCall)]
96 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
98 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
100 if (fromNoninstanciated == null)
101 throw new ArgumentNullException ("fromNoninstanciated");
102 return GetCorrespondingInflatedMethod (fromNoninstanciated);
105 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
107 if (fromNoninstanciated == null)
108 throw new ArgumentNullException ("fromNoninstanciated");
109 return GetCorrespondingInflatedConstructor (fromNoninstanciated);
112 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
114 /* create sensible flags from given FieldInfo */
115 BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
116 flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
117 return GetField (fromNoninstanciated.Name, flags);
120 string GetDefaultMemberName ()
122 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
123 return att.Length != 0 ? ((DefaultMemberAttribute) att [0]).MemberName : null;
126 RuntimeConstructorInfo m_serializationCtor;
127 internal RuntimeConstructorInfo GetSerializationCtor()
129 if (m_serializationCtor == null) {
130 var s_SICtorParamTypes = new Type[] { typeof(SerializationInfo), typeof(StreamingContext) };
132 m_serializationCtor = GetConstructor(
133 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
134 null,
135 CallingConventions.Any,
136 s_SICtorParamTypes,
137 null) as RuntimeConstructorInfo;
140 return m_serializationCtor;
143 internal Object CreateInstanceSlow(bool publicOnly, bool wrapExceptions, bool skipCheckThis, bool fillCache)
145 //bool bNeedSecurityCheck = true;
146 //bool bCanBeCached = false;
147 //bool bSecurityCheckOff = false;
149 if (!skipCheckThis)
150 CreateInstanceCheckThis();
152 //if (!fillCache)
153 // bSecurityCheckOff = true;
155 return CreateInstanceMono (!publicOnly, wrapExceptions);
158 object CreateInstanceMono (bool nonPublic, bool wrapExceptions)
160 var ctor = GetDefaultConstructor ();
161 if (!nonPublic && ctor != null && !ctor.IsPublic) {
162 ctor = null;
165 if (ctor == null) {
166 Type elementType = this.GetRootElementType();
167 if (ReferenceEquals (elementType, typeof (TypedReference)) || ReferenceEquals (elementType, typeof (RuntimeArgumentHandle)))
168 throw new NotSupportedException (Environment.GetResourceString ("NotSupported_ContainsStackPtr"));
170 if (IsValueType)
171 return CreateInstanceInternal (this);
173 throw new MissingMethodException ("Default constructor not found for type " + FullName);
176 // TODO: .net does more checks in unmanaged land in RuntimeTypeHandle::CreateInstance
177 if (IsAbstract) {
178 throw new MissingMethodException ("Cannot create an abstract class '{0}'.", FullName);
181 return ctor.InternalInvoke (null, null, wrapExceptions);
184 internal Object CheckValue (Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
186 bool failed = false;
187 var res = TryConvertToType (value, ref failed);
188 if (!failed)
189 return res;
191 if ((invokeAttr & BindingFlags.ExactBinding) == BindingFlags.ExactBinding)
192 throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_ObjObjEx"), value.GetType(), this));
194 if (binder != null && binder != Type.DefaultBinder)
195 return binder.ChangeType (value, this, culture);
197 throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_ObjObjEx"), value.GetType(), this));
200 object TryConvertToType (object value, ref bool failed)
202 if (IsInstanceOfType (value)) {
203 return value;
206 if (IsByRef) {
207 var elementType = GetElementType ();
208 if (value == null || elementType.IsInstanceOfType (value)) {
209 return value;
213 if (value == null)
214 return value;
216 if (IsEnum) {
217 var type = Enum.GetUnderlyingType (this);
218 if (type == value.GetType ())
219 return value;
220 var res = IsConvertibleToPrimitiveType (value, this);
221 if (res != null)
222 return res;
223 } else if (IsPrimitive) {
224 var res = IsConvertibleToPrimitiveType (value, this);
225 if (res != null)
226 return res;
227 } else if (IsPointer) {
228 var vtype = value.GetType ();
229 if (vtype == typeof (IntPtr) || vtype == typeof (UIntPtr))
230 return value;
233 failed = true;
234 return null;
237 // Binder uses some incompatible conversion rules. For example
238 // int value cannot be used with decimal parameter but in other
239 // ways it's more flexible than normal convertor, for example
240 // long value can be used with int based enum
241 static object IsConvertibleToPrimitiveType (object value, Type targetType)
243 var type = value.GetType ();
244 if (type.IsEnum) {
245 type = Enum.GetUnderlyingType (type);
246 if (type == targetType)
247 return value;
250 var from = Type.GetTypeCode (type);
251 var to = Type.GetTypeCode (targetType);
253 switch (to) {
254 case TypeCode.Char:
255 switch (from) {
256 case TypeCode.Byte:
257 return (Char) (Byte) value;
258 case TypeCode.UInt16:
259 return value;
261 break;
262 case TypeCode.Int16:
263 switch (from) {
264 case TypeCode.Byte:
265 return (Int16) (Byte) value;
266 case TypeCode.SByte:
267 return (Int16) (SByte) value;
269 break;
270 case TypeCode.UInt16:
271 switch (from) {
272 case TypeCode.Byte:
273 return (UInt16) (Byte) value;
274 case TypeCode.Char:
275 return value;
277 break;
278 case TypeCode.Int32:
279 switch (from) {
280 case TypeCode.Byte:
281 return (Int32) (Byte) value;
282 case TypeCode.SByte:
283 return (Int32) (SByte) value;
284 case TypeCode.Char:
285 return (Int32) (Char) value;
286 case TypeCode.Int16:
287 return (Int32) (Int16) value;
288 case TypeCode.UInt16:
289 return (Int32) (UInt16) value;
291 break;
292 case TypeCode.UInt32:
293 switch (from) {
294 case TypeCode.Byte:
295 return (UInt32) (Byte) value;
296 case TypeCode.Char:
297 return (UInt32) (Char) value;
298 case TypeCode.UInt16:
299 return (UInt32) (UInt16) value;
301 break;
302 case TypeCode.Int64:
303 switch (from) {
304 case TypeCode.Byte:
305 return (Int64) (Byte) value;
306 case TypeCode.SByte:
307 return (Int64) (SByte) value;
308 case TypeCode.Int16:
309 return (Int64) (Int16) value;
310 case TypeCode.Char:
311 return (Int64) (Char) value;
312 case TypeCode.UInt16:
313 return (Int64) (UInt16) value;
314 case TypeCode.Int32:
315 return (Int64) (Int32) value;
316 case TypeCode.UInt32:
317 return (Int64) (UInt32) value;
319 break;
320 case TypeCode.UInt64:
321 switch (from) {
322 case TypeCode.Byte:
323 return (UInt64) (Byte) value;
324 case TypeCode.Char:
325 return (UInt64) (Char) value;
326 case TypeCode.UInt16:
327 return (UInt64) (UInt16) value;
328 case TypeCode.UInt32:
329 return (UInt64) (UInt32) value;
331 break;
332 case TypeCode.Single:
333 switch (from) {
334 case TypeCode.Byte:
335 return (Single) (Byte) value;
336 case TypeCode.SByte:
337 return (Single) (SByte) value;
338 case TypeCode.Int16:
339 return (Single) (Int16) value;
340 case TypeCode.Char:
341 return (Single) (Char) value;
342 case TypeCode.UInt16:
343 return (Single) (UInt16) value;
344 case TypeCode.Int32:
345 return (Single) (Int32) value;
346 case TypeCode.UInt32:
347 return (Single) (UInt32) value;
348 case TypeCode.Int64:
349 return (Single) (Int64) value;
350 case TypeCode.UInt64:
351 return (Single) (UInt64) value;
353 break;
354 case TypeCode.Double:
355 switch (from) {
356 case TypeCode.Byte:
357 return (Double) (Byte) value;
358 case TypeCode.SByte:
359 return (Double) (SByte) value;
360 case TypeCode.Char:
361 return (Double) (Char) value;
362 case TypeCode.Int16:
363 return (Double) (Int16) value;
364 case TypeCode.UInt16:
365 return (Double) (UInt16) value;
366 case TypeCode.Int32:
367 return (Double) (Int32) value;
368 case TypeCode.UInt32:
369 return (Double) (UInt32) value;
370 case TypeCode.Int64:
371 return (Double) (Int64) value;
372 case TypeCode.UInt64:
373 return (Double) (UInt64) value;
374 case TypeCode.Single:
375 return (Double) (Single) value;
377 break;
380 // Everything else is rejected
381 return null;
384 string GetCachedName (TypeNameKind kind)
386 switch (kind) {
387 case TypeNameKind.SerializationName:
388 return ToString ();
389 default:
390 throw new NotImplementedException ();
394 [MethodImplAttribute(MethodImplOptions.InternalCall)]
395 extern Type make_array_type (int rank);
397 public override Type MakeArrayType ()
399 return make_array_type (0);
402 public override Type MakeArrayType (int rank)
404 if (rank < 1 || rank > 255)
405 throw new IndexOutOfRangeException ();
406 return make_array_type (rank);
409 [MethodImplAttribute(MethodImplOptions.InternalCall)]
410 extern Type make_byref_type ();
412 public override Type MakeByRefType ()
414 if (IsByRef)
415 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
416 return make_byref_type ();
419 [MethodImplAttribute(MethodImplOptions.InternalCall)]
420 static extern Type MakePointerType (Type type);
422 public override Type MakePointerType ()
424 if (IsByRef)
425 throw new TypeLoadException ($"Could not load type '{GetType()}' from assembly '{AssemblyQualifiedName}");
426 return MakePointerType (this);
429 public override StructLayoutAttribute StructLayoutAttribute {
430 get {
431 return StructLayoutAttribute.GetCustomAttribute (this);
435 public override bool ContainsGenericParameters {
436 get {
437 if (IsGenericParameter)
438 return true;
440 if (IsGenericType) {
441 foreach (Type arg in GetGenericArguments ())
442 if (arg.ContainsGenericParameters)
443 return true;
446 if (HasElementType)
447 return GetElementType ().ContainsGenericParameters;
449 return false;
453 public override Type[] GetGenericParameterConstraints()
455 if (!IsGenericParameter)
456 throw new InvalidOperationException(Environment.GetResourceString("Arg_NotGenericParameter"));
457 Contract.EndContractBlock();
459 var paramInfo = new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this));
460 Type[] constraints = paramInfo.Constraints;
462 if (constraints == null)
463 constraints = EmptyArray<Type>.Value;
465 return constraints;
468 internal static object CreateInstanceForAnotherGenericParameter (Type genericType, RuntimeType genericArgument)
470 var gt = (RuntimeType) MakeGenericType (genericType, new Type [] { genericArgument });
471 var ctor = gt.GetDefaultConstructor ();
472 return ctor.InternalInvoke (null, null, wrapExceptions: true);
475 [MethodImplAttribute(MethodImplOptions.InternalCall)]
476 static extern Type MakeGenericType (Type gt, Type [] types);
478 [MethodImplAttribute(MethodImplOptions.InternalCall)]
479 internal extern IntPtr GetMethodsByName_native (IntPtr namePtr, BindingFlags bindingAttr, MemberListType listType);
481 internal RuntimeMethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
483 var refh = new RuntimeTypeHandle (reflectedType);
484 using (var namePtr = new Mono.SafeStringMarshal (name))
485 using (var h = new Mono.SafeGPtrArrayHandle (GetMethodsByName_native (namePtr.Value, bindingAttr, listType))) {
486 var n = h.Length;
487 var a = new RuntimeMethodInfo [n];
488 for (int i = 0; i < n; i++) {
489 var mh = new RuntimeMethodHandle (h[i]);
490 a[i] = (RuntimeMethodInfo) RuntimeMethodInfo.GetMethodFromHandleNoGenericCheck (mh, refh);
492 return a;
496 [MethodImplAttribute(MethodImplOptions.InternalCall)]
497 extern IntPtr GetPropertiesByName_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
499 [MethodImplAttribute(MethodImplOptions.InternalCall)]
500 extern IntPtr GetConstructors_native (BindingFlags bindingAttr);
502 RuntimeConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, RuntimeType reflectedType)
504 var refh = new RuntimeTypeHandle (reflectedType);
505 using (var h = new Mono.SafeGPtrArrayHandle (GetConstructors_native (bindingAttr))) {
506 var n = h.Length;
507 var a = new RuntimeConstructorInfo [n];
508 for (int i = 0; i < n; i++) {
509 var mh = new RuntimeMethodHandle (h[i]);
510 a[i] = (RuntimeConstructorInfo) RuntimeMethodInfo.GetMethodFromHandleNoGenericCheck (mh, refh);
512 return a;
516 RuntimePropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
518 var refh = new RuntimeTypeHandle (reflectedType);
519 using (var namePtr = new Mono.SafeStringMarshal (name))
520 using (var h = new Mono.SafeGPtrArrayHandle (GetPropertiesByName_native (namePtr.Value, bindingAttr, listType))) {
521 var n = h.Length;
522 var a = new RuntimePropertyInfo [n];
523 for (int i = 0; i < n; i++) {
524 var ph = new Mono.RuntimePropertyHandle (h[i]);
525 a[i] = (RuntimePropertyInfo) RuntimePropertyInfo.GetPropertyFromHandle (ph, refh);
527 return a;
531 public override InterfaceMapping GetInterfaceMap (Type ifaceType)
533 if (IsGenericParameter)
534 throw new InvalidOperationException(Environment.GetResourceString("Arg_GenericParameter"));
536 if ((object)ifaceType == null)
537 throw new ArgumentNullException("ifaceType");
538 Contract.EndContractBlock();
540 RuntimeType ifaceRtType = ifaceType as RuntimeType;
542 if (ifaceRtType == null)
543 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "ifaceType");
545 InterfaceMapping res;
546 if (!ifaceType.IsInterface)
547 throw new ArgumentException ("Argument must be an interface.", "ifaceType");
548 if (IsInterface)
549 throw new ArgumentException ("'this' type cannot be an interface itself");
550 res.TargetType = this;
551 res.InterfaceType = ifaceType;
552 GetInterfaceMapData (this, ifaceType, out res.TargetMethods, out res.InterfaceMethods);
553 if (res.TargetMethods == null)
554 throw new ArgumentException ("Interface not found", "ifaceType");
556 return res;
559 [MethodImplAttribute(MethodImplOptions.InternalCall)]
560 static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
562 [MethodImplAttribute (MethodImplOptions.InternalCall)]
563 private extern static void GetGUID (Type type, byte[] guid);
565 public override Guid GUID {
566 get {
567 var guid = new byte [16];
568 GetGUID (this, guid);
569 return new Guid (guid);
573 [MethodImplAttribute(MethodImplOptions.InternalCall)]
574 internal extern void GetPacking (out int packing, out int size);
576 #if MONO_COM
577 private static Dictionary<Guid, Type> clsid_types;
578 private static AssemblyBuilder clsid_assemblybuilder;
579 #endif
581 internal static Type GetTypeFromCLSIDImpl(Guid clsid, String server, bool throwOnError)
583 #if MONO_COM
584 Type result;
586 if (clsid_types == null)
588 Dictionary<Guid, Type> new_clsid_types = new Dictionary<Guid, Type> ();
589 Interlocked.CompareExchange<Dictionary<Guid, Type>>(
590 ref clsid_types, new_clsid_types, null);
593 lock (clsid_types) {
594 if (clsid_types.TryGetValue(clsid, out result))
595 return result;
597 if (clsid_assemblybuilder == null)
599 AssemblyName assemblyname = new AssemblyName ();
600 assemblyname.Name = "GetTypeFromCLSIDDummyAssembly";
601 /* Dynamically created assembly is marked internal to corlib to allow
602 __ComObject access for dynamic types. */
603 clsid_assemblybuilder = new AssemblyBuilder (assemblyname, null,
604 AssemblyBuilderAccess.Run, true);
606 ModuleBuilder modulebuilder = clsid_assemblybuilder.DefineDynamicModule (
607 clsid.ToString ());
609 TypeBuilder typebuilder = modulebuilder.DefineType ("System.__ComObject",
610 TypeAttributes.Public | TypeAttributes.Class, typeof(System.__ComObject));
612 Type[] guidattrtypes = new Type[] { typeof(string) };
614 CustomAttributeBuilder customattr = new CustomAttributeBuilder (
615 typeof(GuidAttribute).GetConstructor (guidattrtypes),
616 new object[] { clsid.ToString () });
618 typebuilder.SetCustomAttribute (customattr);
620 customattr = new CustomAttributeBuilder (
621 typeof(ComImportAttribute).GetConstructor (EmptyTypes),
622 new object[0] {});
624 typebuilder.SetCustomAttribute (customattr);
626 result = typebuilder.CreateType ();
628 clsid_types.Add(clsid, result);
630 return result;
632 #else
633 throw new NotImplementedException ("Unmanaged activation removed");
634 #endif
637 protected override TypeCode GetTypeCodeImpl ()
639 return GetTypeCodeImplInternal (this);
642 [MethodImplAttribute(MethodImplOptions.InternalCall)]
643 extern static TypeCode GetTypeCodeImplInternal (Type type);
645 internal static Type GetTypeFromProgIDImpl(String progID, String server, bool throwOnError)
647 throw new NotImplementedException ("Unmanaged activation is not supported");
650 public override string ToString()
652 return getFullName (false, false);
655 bool IsGenericCOMObjectImpl ()
657 return false;
660 [MethodImplAttribute (MethodImplOptions.InternalCall)]
661 static extern object CreateInstanceInternal (Type type);
663 public extern override MethodBase DeclaringMethod {
664 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665 get;
668 [MethodImplAttribute(MethodImplOptions.InternalCall)]
669 internal extern string getFullName(bool full_name, bool assembly_qualified);
671 [MethodImplAttribute(MethodImplOptions.InternalCall)]
672 extern Type[] GetGenericArgumentsInternal (bool runtimeArray);
674 GenericParameterAttributes GetGenericParameterAttributes () {
675 return (new Mono.RuntimeGenericParamInfoHandle (RuntimeTypeHandle.GetGenericParameterInfo (this))).Attributes;
678 [MethodImplAttribute(MethodImplOptions.InternalCall)]
679 extern int GetGenericParameterPosition ();
681 [MethodImplAttribute(MethodImplOptions.InternalCall)]
682 extern IntPtr GetEvents_native (IntPtr name, MemberListType listType);
684 [MethodImplAttribute(MethodImplOptions.InternalCall)]
685 extern IntPtr GetFields_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
687 RuntimeFieldInfo[] GetFields_internal (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
689 var refh = new RuntimeTypeHandle (reflectedType);
690 using (var namePtr = new Mono.SafeStringMarshal (name))
691 using (var h = new Mono.SafeGPtrArrayHandle (GetFields_native (namePtr.Value, bindingAttr, listType))) {
692 int n = h.Length;
693 var a = new RuntimeFieldInfo[n];
694 for (int i = 0; i < n; i++) {
695 var fh = new RuntimeFieldHandle (h[i]);
696 a[i] = (RuntimeFieldInfo) FieldInfo.GetFieldFromHandle (fh, refh);
698 return a;
702 RuntimeEventInfo[] GetEvents_internal (string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
704 var refh = new RuntimeTypeHandle (reflectedType);
705 using (var namePtr = new Mono.SafeStringMarshal (name))
706 using (var h = new Mono.SafeGPtrArrayHandle (GetEvents_native (namePtr.Value, listType))) {
707 int n = h.Length;
708 var a = new RuntimeEventInfo[n];
709 for (int i = 0; i < n; i++) {
710 var eh = new Mono.RuntimeEventHandle (h[i]);
711 a[i] = (RuntimeEventInfo) EventInfo.GetEventFromHandle (eh, refh);
713 return a;
717 [MethodImplAttribute(MethodImplOptions.InternalCall)]
718 public extern override Type[] GetInterfaces();
720 [MethodImplAttribute(MethodImplOptions.InternalCall)]
721 extern IntPtr GetNestedTypes_native (IntPtr name, BindingFlags bindingAttr, MemberListType listType);
723 RuntimeType[] GetNestedTypes_internal (string displayName, BindingFlags bindingAttr, MemberListType listType)
725 string internalName = null;
726 if (displayName != null)
727 internalName = TypeIdentifiers.FromDisplay (displayName).InternalName;
728 using (var namePtr = new Mono.SafeStringMarshal (internalName))
729 using (var h = new Mono.SafeGPtrArrayHandle (GetNestedTypes_native (namePtr.Value, bindingAttr, listType))) {
730 int n = h.Length;
731 var a = new RuntimeType [n];
732 for (int i = 0; i < n; i++) {
733 var th = new RuntimeTypeHandle (h[i]);
734 a[i] = (RuntimeType) Type.GetTypeFromHandle (th);
736 return a;
740 public override string AssemblyQualifiedName {
741 get {
742 return getFullName (true, true);
746 public extern override Type DeclaringType {
747 [MethodImplAttribute(MethodImplOptions.InternalCall)]
748 get;
751 public extern override string Name {
752 [MethodImplAttribute(MethodImplOptions.InternalCall)]
753 get;
756 public extern override string Namespace {
757 [MethodImplAttribute(MethodImplOptions.InternalCall)]
758 get;
761 #if MOBILE
762 static int get_core_clr_security_level ()
764 return 1;
766 #else
767 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
768 [MethodImplAttribute(MethodImplOptions.InternalCall)]
769 public extern int get_core_clr_security_level ();
771 public override bool IsSecurityTransparent {
772 get { return get_core_clr_security_level () == 0; }
775 public override bool IsSecurityCritical {
776 get { return get_core_clr_security_level () > 0; }
779 public override bool IsSecuritySafeCritical {
780 get { return get_core_clr_security_level () == 1; }
782 #endif
784 public override int GetHashCode()
786 Type t = UnderlyingSystemType;
787 if (t != null && t != this)
788 return t.GetHashCode ();
789 return (int)_impl.Value;
792 public override string FullName {
793 get {
794 // See https://github.com/mono/mono/issues/18180 and
795 // https://github.com/dotnet/runtime/blob/f23e2796ab5f6fea71c9fdacac024822280253db/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs#L1468-L1472
796 if (ContainsGenericParameters && !GetRootElementType().IsGenericTypeDefinition)
797 return null;
799 string fullName;
800 // This doesn't need locking
801 if (type_info == null)
802 type_info = new MonoTypeInfo ();
803 if ((fullName = type_info.full_name) == null)
804 fullName = type_info.full_name = getFullName (true, false);
806 return fullName;
810 public sealed override bool HasSameMetadataDefinitionAs (MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeType> (other);
812 public override bool IsSZArray {
813 get {
814 // TODO: intrinsic
815 return IsArray && ReferenceEquals (this, GetElementType ().MakeArrayType ());
819 internal override bool IsUserType {
820 get {
821 return false;
825 [System.Runtime.InteropServices.ComVisible(true)]
826 [Pure]
827 public override bool IsSubclassOf(Type type)
829 if ((object)type == null)
830 throw new ArgumentNullException("type");
831 Contract.EndContractBlock();
832 RuntimeType rtType = type as RuntimeType;
833 if (rtType == null)
834 return false;
836 return RuntimeTypeHandle.IsSubclassOf (this, rtType);
839 public override bool IsByRefLike {
840 get {
841 return RuntimeTypeHandle.IsByRefLike (this);
845 public override bool IsTypeDefinition => RuntimeTypeHandle.IsTypeDefinition (this);