In Test/System.Windows.Forms:
[mono-project.git] / mcs / class / corlib / System / ModuleHandle.cs
blobb501882ea87377c0d58cccf5fd4d1abd4b2ecfc4
1 //
2 // System.ModuleHandle.cs
3 //
4 // Author:
5 // Zoltan Varga (vargaz@gmail.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
38 #if NET_2_0
40 using System.Runtime.ConstrainedExecution;
42 namespace System
44 [ComVisible (true)]
45 public struct ModuleHandle
47 IntPtr value;
49 public static readonly ModuleHandle EmptyHandle = new ModuleHandle (IntPtr.Zero);
51 internal ModuleHandle (IntPtr v)
53 value = v;
56 internal IntPtr Value {
57 get {
58 return value;
62 internal void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
64 if (value == IntPtr.Zero)
65 throw new ArgumentNullException (String.Empty, "Invalid handle");
66 Module.GetPEKind (value, out peKind, out machine);
69 public RuntimeFieldHandle ResolveFieldHandle (int fieldToken)
71 ResolveTokenError error;
72 if (value == IntPtr.Zero)
73 throw new ArgumentNullException (String.Empty, "Invalid handle");
74 IntPtr res = Module.ResolveFieldToken (value, fieldToken, out error);
75 if (res == IntPtr.Zero)
76 throw new Exception (String.Format ("Could not load field '0x{0:x}' from assembly '0x{1:x}'", fieldToken, value.ToInt64 ()));
77 else
78 return new RuntimeFieldHandle (res);
81 public RuntimeMethodHandle ResolveMethodHandle (int methodToken)
83 ResolveTokenError error;
84 if (value == IntPtr.Zero)
85 throw new ArgumentNullException (String.Empty, "Invalid handle");
86 IntPtr res = Module.ResolveMethodToken (value, methodToken, out error);
87 if (res == IntPtr.Zero)
88 throw new Exception (String.Format ("Could not load method '0x{0:x}' from assembly '0x{1:x}'", methodToken, value.ToInt64 ()));
89 else
90 return new RuntimeMethodHandle (res);
93 public RuntimeTypeHandle ResolveTypeHandle (int typeToken)
95 ResolveTokenError error;
96 if (value == IntPtr.Zero)
97 throw new ArgumentNullException (String.Empty, "Invalid handle");
98 IntPtr res = Module.ResolveTypeToken (value, typeToken, out error);
99 if (res == IntPtr.Zero)
100 throw new TypeLoadException (String.Format ("Could not load type '0x{0:x}' from assembly '0x{1:x}'", typeToken, value.ToInt64 ()));
101 else
102 return new RuntimeTypeHandle (res);
105 public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken (int fieldToken) {
106 return ResolveFieldHandle (fieldToken);
109 public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken (int methodToken)
111 return ResolveMethodHandle (methodToken);
114 public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken (int typeToken)
116 return ResolveTypeHandle (typeToken);
119 #if NET_2_0
120 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
121 #endif
122 public override bool Equals (object obj)
124 if (obj == null || GetType () != obj.GetType ())
125 return false;
127 return value == ((ModuleHandle)obj).Value;
130 #if NET_2_0
131 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
132 #endif
133 public bool Equals (ModuleHandle handle)
135 return value == handle.Value;
138 public override int GetHashCode ()
140 return value.GetHashCode ();
145 #endif