bff94af25a8a51815e0bf7430856adbc0015fe2f
[mono.git] / mcs / class / corlib / System / RuntimeMethodHandle.cs
1 //
2 // System.RuntimeMethodHandle.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Reflection;
32 using System.Runtime.Serialization;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
36
37 #if NET_2_0
38 using System.Runtime.ConstrainedExecution;
39 #endif
40
41 namespace System
42 {
43 #if NET_2_0
44         [ComVisible (true)]
45 #endif
46         [MonoTODO ("Serialization needs tests")]
47         [Serializable]
48         public struct RuntimeMethodHandle : ISerializable
49         {
50                 IntPtr value;
51
52                 internal RuntimeMethodHandle (IntPtr v)
53                 {
54                         value = v;
55                 }
56
57                 RuntimeMethodHandle (SerializationInfo info, StreamingContext context)
58                 {
59                         if (info == null)
60                                 throw new ArgumentNullException ("info");
61
62                         MonoMethod mm = ((MonoMethod) info.GetValue ("MethodObj", typeof (MonoMethod)));
63                         value = mm.MethodHandle.Value;
64                         if (value == IntPtr.Zero)
65                                 throw new SerializationException (Locale.GetText ("Insufficient state."));
66                 }
67
68                 public IntPtr Value {
69                         get {
70                                 return value;
71                         }
72                 }
73
74                 // This is from ISerializable
75                 public void GetObjectData (SerializationInfo info, StreamingContext context)
76                 {
77                         if (info == null)
78                                 throw new ArgumentNullException ("info");
79
80                         info.AddValue ("MethodObj", (MonoMethod) MethodBase.GetMethodFromHandle (this), typeof (MonoMethod));
81                 }
82
83                 [MethodImpl (MethodImplOptions.InternalCall)]
84                 static extern IntPtr GetFunctionPointer (IntPtr m);
85
86                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
87                 public IntPtr GetFunctionPointer ()
88                 {
89                         return GetFunctionPointer (value);
90                 }
91
92 #if NET_2_0
93                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
94                 public override bool Equals (object obj)
95                 {
96                         if (obj == null || GetType () != obj.GetType ())
97                                 return false;
98
99                         return value == ((RuntimeMethodHandle)obj).Value;
100                 }
101
102                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
103                 public bool Equals (RuntimeMethodHandle handle)
104                 {
105                         return value == handle.Value;
106                 }
107
108                 public override int GetHashCode ()
109                 {
110                         return value.GetHashCode ();
111                 }
112
113                 public static bool operator == (RuntimeMethodHandle left, RuntimeMethodHandle right)
114                 {
115                         return left.Equals (right);
116                 }
117
118                 public static bool operator != (RuntimeMethodHandle left, RuntimeMethodHandle right)
119                 {
120                         return !left.Equals (right);
121                 }
122 #endif
123         }
124 }