[bcl] Implement RuntimeHelpers.IsReferenceOrContainsReferences () as an icall, so...
[mono-project.git] / mcs / class / corlib / System / RuntimeTypeHandle.cs
blobde5681504fad65b6282cd22a555ae64bf75f3f96
1 //
2 // System.RuntimeTypeHandle.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc. http://www.ximian.com
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Runtime.Serialization;
35 using System.Runtime.InteropServices;
36 using System.Runtime.ConstrainedExecution;
37 using System.Threading;
38 using System.Runtime.CompilerServices;
39 using System.Reflection;
40 using System.Diagnostics.Contracts;
42 namespace System
44 [ComVisible (true)]
45 [Serializable]
46 public struct RuntimeTypeHandle : ISerializable
48 IntPtr value;
50 internal RuntimeTypeHandle (IntPtr val)
52 value = val;
55 internal RuntimeTypeHandle (RuntimeType type)
56 : this (type._impl.value)
60 RuntimeTypeHandle (SerializationInfo info, StreamingContext context)
62 if (info == null)
63 throw new ArgumentNullException ("info");
65 RuntimeType mt = ((RuntimeType) info.GetValue ("TypeObj", typeof (RuntimeType)));
66 value = mt.TypeHandle.Value;
67 if (value == IntPtr.Zero)
68 throw new SerializationException (Locale.GetText ("Insufficient state."));
71 public IntPtr Value {
72 get {
73 return value;
77 public void GetObjectData (SerializationInfo info, StreamingContext context)
79 if (info == null)
80 throw new ArgumentNullException ("info");
82 if (value == IntPtr.Zero)
83 throw new SerializationException ("Object fields may not be properly initialized");
85 info.AddValue ("TypeObj", Type.GetTypeHandle (this), typeof (RuntimeType));
88 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
89 public override bool Equals (object obj)
91 if (obj == null || GetType () != obj.GetType ())
92 return false;
94 return value == ((RuntimeTypeHandle)obj).Value;
97 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
98 public bool Equals (RuntimeTypeHandle handle)
100 return value == handle.Value;
103 public override int GetHashCode ()
105 return value.GetHashCode ();
108 public static bool operator == (RuntimeTypeHandle left, Object right)
110 return (right != null) && (right is RuntimeTypeHandle) && left.Equals ((RuntimeTypeHandle)right);
113 public static bool operator != (RuntimeTypeHandle left, Object right)
115 return (right == null) || !(right is RuntimeTypeHandle) || !left.Equals ((RuntimeTypeHandle)right);
118 public static bool operator == (Object left, RuntimeTypeHandle right)
120 return (left != null) && (left is RuntimeTypeHandle) && ((RuntimeTypeHandle)left).Equals (right);
123 public static bool operator != (Object left, RuntimeTypeHandle right)
125 return (left == null) || !(left is RuntimeTypeHandle) || !((RuntimeTypeHandle)left).Equals (right);
128 [MethodImplAttribute(MethodImplOptions.InternalCall)]
129 internal static extern TypeAttributes GetAttributes (RuntimeType type);
131 [CLSCompliant (false)]
132 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
133 public ModuleHandle GetModuleHandle ()
135 // Although MS' runtime is crashing here, we prefer throwing an exception.
136 // The check is needed because Type.GetTypeFromHandle returns null
137 // for zero handles.
138 if (value == IntPtr.Zero)
139 throw new InvalidOperationException ("Object fields may not be properly initialized");
141 return Type.GetTypeFromHandle (this).Module.ModuleHandle;
144 [MethodImplAttribute(MethodImplOptions.InternalCall)]
145 static extern int GetMetadataToken (RuntimeType type);
147 internal static int GetToken (RuntimeType type)
149 return GetMetadataToken (type);
152 [MethodImplAttribute(MethodImplOptions.InternalCall)]
153 extern static Type GetGenericTypeDefinition_impl (RuntimeType type);
155 internal static Type GetGenericTypeDefinition (RuntimeType type)
157 return GetGenericTypeDefinition_impl (type);
160 internal static bool HasElementType (RuntimeType type)
162 return IsArray (type) || IsByRef (type) || IsPointer (type);
165 internal static bool HasProxyAttribute (RuntimeType type)
167 throw new NotImplementedException ("HasProxyAttribute");
170 [MethodImplAttribute(MethodImplOptions.InternalCall)]
171 internal extern static bool HasInstantiation (RuntimeType type);
173 [MethodImplAttribute(MethodImplOptions.InternalCall)]
174 internal extern static bool IsArray(RuntimeType type);
176 [MethodImplAttribute(MethodImplOptions.InternalCall)]
177 internal extern static bool IsByRef (RuntimeType type);
179 [MethodImplAttribute (MethodImplOptions.InternalCall)]
180 internal extern static bool IsComObject (RuntimeType type);
182 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183 internal extern static bool IsInstanceOfType (RuntimeType type, Object o);
185 [MethodImplAttribute(MethodImplOptions.InternalCall)]
186 internal extern static bool IsPointer (RuntimeType type);
188 [MethodImplAttribute(MethodImplOptions.InternalCall)]
189 internal extern static bool IsPrimitive (RuntimeType type);
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)
201 return typeof (ContextBoundObject).IsAssignableFrom (type);
204 internal static bool IsEquivalentTo (RuntimeType rtType1, RuntimeType rtType2)
206 // refence check is done earlier and we don't recognize anything else
207 return false;
210 internal static bool IsSzArray(RuntimeType type)
212 // TODO: Better check
213 return IsArray (type) && type.GetArrayRank () == 1;
216 internal static bool IsInterface (RuntimeType type)
218 return (type.Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
221 [MethodImplAttribute(MethodImplOptions.InternalCall)]
222 internal extern static int GetArrayRank(RuntimeType type);
224 [MethodImplAttribute(MethodImplOptions.InternalCall)]
225 internal extern static RuntimeAssembly GetAssembly (RuntimeType type);
227 [MethodImplAttribute(MethodImplOptions.InternalCall)]
228 internal extern static RuntimeType GetElementType (RuntimeType type);
230 [MethodImplAttribute(MethodImplOptions.InternalCall)]
231 internal extern static RuntimeModule GetModule (RuntimeType type);
233 [MethodImplAttribute(MethodImplOptions.InternalCall)]
234 internal extern static bool IsGenericVariable (RuntimeType type);
236 [MethodImplAttribute(MethodImplOptions.InternalCall)]
237 internal extern static RuntimeType GetBaseType (RuntimeType type);
239 internal static bool CanCastTo (RuntimeType type, RuntimeType target)
241 return type_is_assignable_from (target, type);
244 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245 static extern bool type_is_assignable_from (Type a, Type b);
247 [MethodImplAttribute(MethodImplOptions.InternalCall)]
248 internal extern static bool IsGenericTypeDefinition (RuntimeType type);
250 [MethodImplAttribute(MethodImplOptions.InternalCall)]
251 internal extern static IntPtr GetGenericParameterInfo (RuntimeType type);