1 // System.Runtime.CompilerServices.RuntimeHelpers
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
6 // (C) Ximian, Inc. 2001
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.Versioning;
32 using System.Runtime.ConstrainedExecution;
33 using System.Reflection;
35 namespace System.Runtime.CompilerServices
37 public static class RuntimeHelpers
39 public delegate void TryCode (Object userData);
41 public delegate void CleanupCode (Object userData, bool exceptionThrown);
43 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44 static extern void InitializeArray (Array array, IntPtr fldHandle);
46 public static void InitializeArray (Array array, RuntimeFieldHandle fldHandle)
48 if ((array == null) || (fldHandle.Value == IntPtr.Zero))
49 throw new ArgumentNullException ();
51 InitializeArray (array, fldHandle.Value);
54 public static extern int OffsetToStringData {
55 [MethodImpl (MethodImplOptions.InternalCall)]
59 public static int GetHashCode (object o) {
60 return Object.InternalGetHashCode (o);
63 public static new bool Equals (object o1, object o2) {
64 // LAMESPEC: According to MSDN, this is equivalent to
65 // Object::Equals (). But the MS version of Object::Equals()
66 // includes the functionality of ValueType::Equals(), while
67 // our version does not.
70 if ((o1 == null) || (o2 == null))
73 return ValueType.DefaultEquals (o1, o2);
75 return Object.Equals (o1, o2);
78 [MethodImplAttribute(MethodImplOptions.InternalCall)]
79 public static extern object GetObjectValue (object obj);
81 [MethodImplAttribute(MethodImplOptions.InternalCall)]
82 static extern void RunClassConstructor (IntPtr type);
84 public static void RunClassConstructor (RuntimeTypeHandle type)
86 if (type.Value == IntPtr.Zero)
87 throw new ArgumentException ("Handle is not initialized.", "type");
89 RunClassConstructor (type.Value);
92 [MethodImplAttribute (MethodImplOptions.InternalCall)]
93 static extern bool SufficientExecutionStack ();
95 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
96 public static void EnsureSufficientExecutionStack ()
98 if (SufficientExecutionStack ())
100 throw new InsufficientExecutionStackException ();
103 public static bool TryEnsureSufficientExecutionStack ()
105 return SufficientExecutionStack ();
108 [MonoTODO("Currently a no-op")]
109 public static void ExecuteCodeWithGuaranteedCleanup (TryCode code, CleanupCode backoutCode, Object userData)
113 [MonoTODO("Currently a no-op")]
114 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
115 public static void PrepareConstrainedRegions ()
119 [MonoTODO("Currently a no-op")]
120 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
121 public static void PrepareConstrainedRegionsNoOP ()
125 [MonoTODO("Currently a no-op")]
126 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
127 public static void ProbeForSufficientStack()
131 // This method triggers a given delegate to be prepared. This involves preparing the
132 // delegate's Invoke method and preparing the target of that Invoke. In the case of
133 // a multi-cast delegate, we rely on the fact that each individual component was prepared
134 // prior to the Combine. In other words, this service does not navigate through the
135 // entire multicasting list.
136 // If our own reliable event sinks perform the Combine (for example AppDomain.DomainUnload),
137 // then the result is fully prepared. But if a client calls Combine himself and then
138 // then adds that combination to e.g. AppDomain.DomainUnload, then the client is responsible
139 // for his own preparation.
140 [System.Security.SecurityCritical] // auto-generated_required
141 [MonoTODO("Currently a no-op")]
142 public static void PrepareDelegate (Delegate d)
145 throw new ArgumentNullException ("d");
148 // extracted from ../../../../external/referencesource/mscorlib/system/runtime/compilerservices/runtimehelpers.cs
150 // See comment above for PrepareDelegate
152 // PrepareContractedDelegate weakens this a bit by only assuring that we prepare
153 // delegates which also have a ReliabilityContract. This is useful for services that
154 // want to provide opt-in reliability, generally some random event sink providing
155 // always reliable semantics to random event handlers that are likely to have not
156 // been written with relability in mind is a lost cause anyway.
158 // NOTE: that for the NGen case you can sidestep the required ReliabilityContract
159 // by using the [PrePrepareMethod] attribute.
160 [System.Security.SecurityCritical] // auto-generated_required
161 [ResourceExposure(ResourceScope.None)]
162 [MonoTODO("Currently a no-op")]
163 public static void PrepareContractedDelegate(Delegate d)
167 [MonoTODO("Currently a no-op")]
168 public static void PrepareMethod (RuntimeMethodHandle method)
172 [MonoTODO("Currently a no-op")]
173 public static void PrepareMethod (RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation)
177 public static void RunModuleConstructor (ModuleHandle module)
179 if (module == ModuleHandle.EmptyHandle)
180 throw new ArgumentException ("Handle is not initialized.", "module");
182 RunModuleConstructor (module.Value);
185 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186 static extern void RunModuleConstructor (IntPtr module);
188 // This is implemented as a JIT intrinsic
189 public static bool IsReferenceOrContainsReferences<T>()
191 return !typeof (T).IsValueType || RuntimeTypeHandle.HasReferences ((typeof (T) as RuntimeType));