2005-06-09 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / ModuleHandle.cs
1 //
2 // System.ModuleHandle.cs
3 //
4 // Author:
5 //   Zoltan Varga (vargaz@gmail.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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.
31 //
32
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37
38 #if NET_2_0
39
40 using System.Runtime.ConstrainedExecution;
41
42 namespace System
43 {
44         [ComVisible (true)]
45         public struct ModuleHandle
46         {
47                 IntPtr value;
48
49                 public static readonly ModuleHandle EmptyHandle = new ModuleHandle (IntPtr.Zero);
50
51                 internal ModuleHandle (IntPtr v)
52                 {
53                         value = v;
54                 }
55
56                 internal IntPtr Value {
57                         get {
58                                 return value;
59                         }
60                 }
61
62                 public void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
63                 {
64                         if (value == IntPtr.Zero)
65                                 throw new ArgumentNullException (String.Empty, "Invalid handle");
66                         Module.GetPEKind (value, out peKind, out machine);
67                 }
68
69                 public RuntimeFieldHandle ResolveFieldHandle (int fieldToken)
70                 {
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);
79                 }
80
81                 public RuntimeMethodHandle ResolveMethodHandle (int methodToken)
82                 {
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);
91                 }
92
93                 public RuntimeTypeHandle ResolveTypeHandle (int typeToken)
94                 {
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);
103                 }
104
105                 [Obsolete]
106                 public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken (int fieldToken) {
107                         return ResolveFieldHandle (fieldToken);
108                 }
109
110                 [Obsolete]
111                 public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken (int methodToken)
112                 {
113                         return ResolveMethodHandle (methodToken);
114                 }
115
116                 [Obsolete]
117                 public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken (int typeToken)
118                 {
119                         return ResolveTypeHandle (typeToken);
120                 }
121
122 #if NET_2_0
123                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
124 #endif
125                 public override bool Equals (object obj)
126                 {
127                         if (obj == null || GetType () != obj.GetType ())
128                                 return false;
129
130                         return value == ((ModuleHandle)obj).Value;
131                 }
132
133 #if NET_2_0
134                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
135 #endif
136                 public bool Equals (ModuleHandle handle)
137                 {
138                         return value == handle.Value;
139                 }
140
141                 public override int GetHashCode ()
142                 {
143                         return value.GetHashCode ();
144                 }
145         }
146 }
147
148 #endif