3 // Miguel de Icaza (miguel@ximian.com)
4 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Runtime
.Serialization
;
32 using System
.Runtime
.CompilerServices
;
33 using System
.Reflection
;
34 using System
.Threading
;
39 public struct RuntimeTypeHandle
: ISerializable
41 readonly IntPtr
value;
43 internal RuntimeTypeHandle (IntPtr val
)
48 internal RuntimeTypeHandle (RuntimeType type
)
49 : this (type
._impl
.value)
53 RuntimeTypeHandle (SerializationInfo info
, StreamingContext context
)
55 throw new PlatformNotSupportedException ();
64 public void GetObjectData (SerializationInfo info
, StreamingContext context
)
66 throw new PlatformNotSupportedException ();
69 public override bool Equals (object? obj
)
71 if (obj
== null || GetType () != obj
.GetType ())
74 return value == ((RuntimeTypeHandle
)obj
).Value
;
77 public bool Equals (RuntimeTypeHandle handle
)
79 return value == handle
.Value
;
82 public override int GetHashCode ()
84 return value.GetHashCode ();
87 public static bool operator == (RuntimeTypeHandle left
, Object right
)
89 return (right
!= null) && (right
is RuntimeTypeHandle
) && left
.Equals ((RuntimeTypeHandle
)right
);
92 public static bool operator != (RuntimeTypeHandle left
, Object right
)
94 return (right
== null) || !(right
is RuntimeTypeHandle
) || !left
.Equals ((RuntimeTypeHandle
)right
);
97 public static bool operator == (Object left
, RuntimeTypeHandle right
)
99 return (left
!= null) && (left
is RuntimeTypeHandle
) && ((RuntimeTypeHandle
)left
).Equals (right
);
102 public static bool operator != (Object left
, RuntimeTypeHandle right
)
104 return (left
== null) || !(left
is RuntimeTypeHandle
) || !((RuntimeTypeHandle
)left
).Equals (right
);
107 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
108 internal static extern TypeAttributes
GetAttributes (RuntimeType type
);
110 [CLSCompliant (false)]
111 public ModuleHandle
GetModuleHandle ()
113 // Although MS' runtime is crashing here, we prefer throwing an exception.
114 // The check is needed because Type.GetTypeFromHandle returns null
116 if (value == IntPtr
.Zero
)
117 throw new InvalidOperationException ("Object fields may not be properly initialized");
119 return Type
.GetTypeFromHandle (this).Module
.ModuleHandle
;
122 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
123 static extern int GetMetadataToken (RuntimeType type
);
125 internal static int GetToken (RuntimeType type
)
127 return GetMetadataToken (type
);
130 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
131 extern static Type
GetGenericTypeDefinition_impl (RuntimeType type
);
133 internal static Type
GetGenericTypeDefinition (RuntimeType type
)
135 return GetGenericTypeDefinition_impl (type
);
138 internal static bool IsPrimitive (RuntimeType type
)
140 CorElementType corElemType
= GetCorElementType (type
);
141 return (corElemType
>= CorElementType
.ELEMENT_TYPE_BOOLEAN
&& corElemType
<= CorElementType
.ELEMENT_TYPE_R8
) ||
142 corElemType
== CorElementType
.ELEMENT_TYPE_I
||
143 corElemType
== CorElementType
.ELEMENT_TYPE_U
;
146 internal static bool IsByRef (RuntimeType type
)
148 CorElementType corElemType
= GetCorElementType (type
);
149 return corElemType
== CorElementType
.ELEMENT_TYPE_BYREF
;
152 internal static bool IsPointer (RuntimeType type
)
154 CorElementType corElemType
= GetCorElementType (type
);
155 return corElemType
== CorElementType
.ELEMENT_TYPE_PTR
;
158 internal static bool IsArray (RuntimeType type
)
160 CorElementType corElemType
= GetCorElementType (type
);
161 return corElemType
== CorElementType
.ELEMENT_TYPE_ARRAY
|| corElemType
== CorElementType
.ELEMENT_TYPE_SZARRAY
;
164 internal static bool IsSzArray (RuntimeType type
)
166 CorElementType corElemType
= GetCorElementType (type
);
167 return corElemType
== CorElementType
.ELEMENT_TYPE_SZARRAY
;
170 internal static bool HasElementType (RuntimeType type
)
172 CorElementType corElemType
= GetCorElementType(type
);
174 return ((corElemType
== CorElementType
.ELEMENT_TYPE_ARRAY
|| corElemType
== CorElementType
.ELEMENT_TYPE_SZARRAY
) // IsArray
175 || (corElemType
== CorElementType
.ELEMENT_TYPE_PTR
) // IsPointer
176 || (corElemType
== CorElementType
.ELEMENT_TYPE_BYREF
)); // IsByRef
179 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
180 internal static extern CorElementType
GetCorElementType (RuntimeType type
);
182 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
183 internal extern static bool HasInstantiation (RuntimeType type
);
185 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
186 internal extern static bool IsComObject (RuntimeType type
);
188 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
189 internal extern static bool IsInstanceOfType (RuntimeType type
, Object o
);
191 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
192 internal extern static bool HasReferences (RuntimeType type
);
194 internal static bool IsComObject (RuntimeType type
, bool isGenericCOM
)
196 return isGenericCOM
? false : IsComObject (type
);
199 internal static bool IsContextful (RuntimeType type
)
204 internal static bool IsEquivalentTo (RuntimeType rtType1
, RuntimeType rtType2
)
206 // refence check is done earlier and we don't recognize anything else
210 internal static bool IsInterface (RuntimeType type
)
212 return (type
.Attributes
& TypeAttributes
.ClassSemanticsMask
) == TypeAttributes
.Interface
;
215 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
216 internal extern static int GetArrayRank(RuntimeType type
);
218 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
219 internal extern static RuntimeAssembly
GetAssembly (RuntimeType type
);
221 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
222 internal extern static RuntimeType
GetElementType (RuntimeType type
);
224 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
225 internal extern static RuntimeModule
GetModule (RuntimeType type
);
227 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
228 internal extern static bool IsGenericVariable (RuntimeType type
);
230 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
231 internal extern static RuntimeType
GetBaseType (RuntimeType type
);
233 internal static bool CanCastTo (RuntimeType type
, RuntimeType target
)
235 return type_is_assignable_from (target
, type
);
238 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
239 static extern bool type_is_assignable_from (Type a
, Type b
);
241 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
242 internal extern static bool IsGenericTypeDefinition (RuntimeType type
);
244 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
245 internal extern static IntPtr
GetGenericParameterInfo (RuntimeType type
);
247 internal static bool IsSubclassOf (RuntimeType childType
, RuntimeType baseType
)
249 return is_subclass_of (childType
._impl
.Value
, baseType
._impl
.Value
);
252 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
253 internal extern static bool is_subclass_of (IntPtr childType
, IntPtr baseType
);
255 [PreserveDependency (".ctor()", "System.Runtime.CompilerServices.IsByRefLikeAttribute")]
256 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
257 internal extern static bool IsByRefLike (RuntimeType type
);
259 internal static bool IsTypeDefinition (RuntimeType type
)
261 // That's how it has been done on CoreFX but we have no GetCorElementType method implementation
262 // see https://github.com/dotnet/coreclr/pull/11355
264 // CorElementType corElemType = GetCorElementType (type);
265 // if (!((corElemType >= CorElementType.Void && corElemType < CorElementType.Ptr) ||
266 // corElemType == CorElementType.ValueType ||
267 // corElemType == CorElementType.Class ||
268 // corElemType == CorElementType.TypedByRef ||
269 // corElemType == CorElementType.I ||
270 // corElemType == CorElementType.U ||
271 // corElemType == CorElementType.Object))
273 // if (HasInstantiation (type) && !IsGenericTypeDefinition (type))
277 // It's like a workaround mentioned in https://github.com/dotnet/corefx/issues/17345
278 return !type
.HasElementType
&& !type
.IsConstructedGenericType
&& !type
.IsGenericParameter
;
281 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
282 static extern RuntimeType
internal_from_name (string name
, ref StackCrawlMark stackMark
, Assembly callerAssembly
, bool throwOnError
, bool ignoreCase
, bool reflectionOnly
);
284 internal static RuntimeType
GetTypeByName (string typeName
, bool throwOnError
, bool ignoreCase
, bool reflectionOnly
, ref StackCrawlMark stackMark
,
285 bool loadTypeFromPartialName
)
287 if (typeName
== null)
288 throw new ArgumentNullException ("typeName");
290 if (typeName
== String
.Empty
)
292 throw new TypeLoadException ("A null or zero length string does not represent a valid Type.");
296 if (reflectionOnly
) {
297 int idx
= typeName
.IndexOf (',');
298 if (idx
< 0 || idx
== 0 || idx
== typeName
.Length
- 1)
299 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
300 string an
= typeName
.Substring (idx
+ 1);
303 a
= Assembly
.ReflectionOnlyLoad (an
);
309 return (RuntimeType
)a
.GetType (typeName
.Substring (0, idx
), throwOnError
, ignoreCase
);
312 var t
= internal_from_name (typeName
, ref stackMark
, null, throwOnError
, ignoreCase
, false);
313 if (throwOnError
&& t
== null)
314 throw new TypeLoadException ("Error loading '" + typeName
+ "'");
318 internal static IntPtr
[] CopyRuntimeTypeHandles (RuntimeTypeHandle
[] inHandles
, out int length
)
320 if (inHandles
== null || inHandles
.Length
== 0) {
325 IntPtr
[] outHandles
= new IntPtr
[inHandles
.Length
];
326 for (int i
= 0; i
< inHandles
.Length
; i
++)
327 outHandles
[i
] = inHandles
[i
].Value
;
328 length
= outHandles
.Length
;