imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / corlib / System / Delegate.cs
1 //
2 // System.Delegate.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Daniel Stodden (stodden@in.tum.de)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //
9 // (C) Ximian, Inc.  http://www.ximian.com
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Reflection;
36 using System.Runtime.Serialization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39
40 namespace System
41 {
42 #if NET_1_1
43         [ClassInterface (ClassInterfaceType.AutoDual)]
44 #endif
45         public abstract class Delegate : ICloneable, ISerializable
46         {
47                 private Type target_type;
48                 private object m_target;
49                 private string method_name;
50                 private IntPtr method_ptr;
51                 private IntPtr delegate_trampoline;
52                 private MethodInfo method_info;
53
54                 protected Delegate (object target, string method)
55                 {
56                         if (target == null)
57                                 throw new ArgumentNullException ("target");
58
59                         if (method == null)
60                                 throw new ArgumentNullException ("method");
61
62                         this.target_type = null;
63                         this.method_ptr = IntPtr.Zero;
64                         this.m_target = target;
65                         this.method_name = method;
66                 }
67
68                 protected Delegate (Type target, string method)
69                 {
70                         if (target == null)
71                                 throw new ArgumentNullException ("target");
72
73                         if (method == null)
74                                 throw new ArgumentNullException ("method");
75
76                         this.target_type = target;
77                         this.method_ptr = IntPtr.Zero;
78                         this.m_target = null;
79                         this.method_name = method;
80                 }
81
82                 public MethodInfo Method {
83                         get {
84                                 return method_info;
85                         }
86                 }
87
88                 public object Target {
89                         get {
90                                 return m_target;
91                         }
92                 }
93
94                 //
95                 // Methods
96                 //
97
98                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
99                 internal static extern Delegate CreateDelegate_internal (Type type, object target, MethodInfo info);
100
101                 public static Delegate CreateDelegate (Type type, MethodInfo method)
102                 {
103                         if (type == null)
104                                 throw new ArgumentNullException ("type");
105
106                         if (method == null)
107                                 throw new ArgumentNullException ("method");
108
109                         if (!type.IsSubclassOf (typeof (MulticastDelegate)))
110                                 throw new ArgumentException ("type is not a subclass of Multicastdelegate");
111
112                         if (!method.IsStatic)
113                                 throw new ArgumentException ("The method should be static.", "method");
114
115                         ParameterInfo[] delargs = type.GetMethod ("Invoke").GetParameters ();
116                         ParameterInfo[] args = method.GetParameters ();
117
118                         if (args.Length != delargs.Length)
119                                 throw new ArgumentException ("method argument length mismatch");
120                         
121                         int length = delargs.Length;
122                         for (int i = 0; i < length; i++)
123                                 if (delargs [i].ParameterType != args [i].ParameterType)
124                                         throw new ArgumentException ("method arguments are incompatible");
125
126                         return CreateDelegate_internal (type, null, method);
127                 }
128
129 #if NET_2_0
130                 public
131 #else
132                 internal
133 #endif
134                 static Delegate CreateDelegate (Type type, object target, MethodInfo method)
135                 {
136                         if (type == null)
137                                 throw new ArgumentNullException ("type");
138
139                         if (method == null)
140                                 throw new ArgumentNullException ("method");
141
142                         if (!type.IsSubclassOf (typeof (MulticastDelegate)))
143                                 throw new ArgumentException ("type is not a subclass of Multicastdelegate");
144
145                         return CreateDelegate_internal (type, target, method);
146
147                 }
148                 
149                 public static Delegate CreateDelegate (Type type, object target, string method)
150                 {
151                         return CreateDelegate(type, target, method, false);
152                 }
153
154                 public static Delegate CreateDelegate (Type type, Type target, string method)
155                 {
156                         if (type == null)
157                                 throw new ArgumentNullException ("type");
158
159                         if (target == null)
160                                 throw new ArgumentNullException ("target");
161
162                         if (method == null)
163                                 throw new ArgumentNullException ("method");
164
165                         if (!type.IsSubclassOf (typeof (MulticastDelegate)))
166                                 throw new ArgumentException ("type is not subclass of MulticastDelegate.");
167
168                         ParameterInfo[] delargs = type.GetMethod ("Invoke").GetParameters ();
169                         Type[] delargtypes = new Type [delargs.Length];
170
171                         for (int i=0; i<delargs.Length; i++)
172                                 delargtypes [i] = delargs [i].ParameterType;
173
174                         /* 
175                          * FIXME: we should check the caller has reflection permission
176                          * or if it lives in the same assembly...
177                          */
178                         BindingFlags flags = BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;
179                         MethodInfo info = target.GetMethod (method, flags, null, delargtypes, new ParameterModifier [0]);
180
181                         if (info == null)
182                                 throw new ArgumentException ("Couldn't bind to method.");
183
184                         return CreateDelegate_internal (type, null, info);
185                 }
186
187                 public static Delegate CreateDelegate (Type type, object target, string method, bool ignoreCase)
188                 {
189                         if (type == null)
190                                 throw new ArgumentNullException ("type");
191
192                         if (target == null)
193                                 throw new ArgumentNullException ("target");
194
195                         if (method == null)
196                                 throw new ArgumentNullException ("method");
197
198                         if (!type.IsSubclassOf (typeof (MulticastDelegate)))
199                                 throw new ArgumentException ("type");
200
201                         ParameterInfo[] delargs = type.GetMethod ("Invoke").GetParameters ();
202                         Type[] delargtypes = new Type [delargs.Length];
203
204                         for (int i=0; i<delargs.Length; i++)
205                                 delargtypes [i] = delargs [i].ParameterType;
206
207                         /* 
208                          * FIXME: we should check the caller has reflection permission
209                          * or if it lives in the same assembly...
210                          */
211                         BindingFlags flags = BindingFlags.ExactBinding | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
212
213                         if (ignoreCase)
214                                 flags |= BindingFlags.IgnoreCase;
215
216                         MethodInfo info = target.GetType ().GetMethod (method, flags, null, delargtypes, new ParameterModifier [0]);
217
218                         if (info == null)
219                                 throw new ArgumentException ("Couldn't bind to method '" + method + "'.");
220
221                         return CreateDelegate_internal (type, target, info);
222                 }
223
224                 public object DynamicInvoke (object[] args)
225                 {
226                         return DynamicInvokeImpl (args);
227                 }
228
229                 protected virtual object DynamicInvokeImpl (object[] args)
230                 {
231                         if (Method == null) {
232                                 Type[] mtypes = new Type [args.Length];
233                                 for (int i = 0; i < args.Length; ++i) {
234                                         mtypes [i] = args [i].GetType ();
235                                 }
236                                 method_info = m_target.GetType ().GetMethod (method_name, mtypes);
237                         }
238                         return Method.Invoke (m_target, args);
239                 }
240
241                 public virtual object Clone ()
242                 {
243                         return MemberwiseClone ();
244                 }
245
246                 public override bool Equals (object obj)
247                 {
248                         Delegate d = obj as Delegate;
249                         
250                         if (d == null)
251                                 return false;
252                         
253                         // Do not compare method_ptr, since it can point to a trampoline
254                         if ((d.target_type == target_type) && (d.m_target == m_target) &&
255                                 (d.method_name == method_name) && (d.method_info == method_info))
256                                 return true;
257
258                         return false;
259                 }
260
261                 public override int GetHashCode ()
262                 {
263                         return (int)method_ptr;
264                 }
265
266                 protected virtual MethodInfo GetMethodImpl ()
267                 {
268                         return Method;
269                 }
270
271                 // This is from ISerializable
272                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
273                 {
274                         DelegateSerializationHolder.GetDelegateData (this, info, context);
275                 }
276
277                 public virtual Delegate[] GetInvocationList()
278                 {
279                         return new Delegate[] { this };
280                 }
281
282                 /// <symmary>
283                 ///   Returns a new MulticastDelegate holding the
284                 ///   concatenated invocation lists of MulticastDelegates a and b
285                 /// </symmary>
286                 public static Delegate Combine (Delegate a, Delegate b)
287                 {
288                         if (a == null) {
289                                 if (b == null)
290                                         return null;
291                                 return b;
292                         } else 
293                                 if (b == null)
294                                         return a;
295
296                         if (a.GetType () != b.GetType ())
297                                 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types."));
298                         
299                         return a.CombineImpl (b);
300                 }
301
302                 /// <symmary>
303                 ///   Returns a new MulticastDelegate holding the
304                 ///   concatenated invocation lists of an Array of MulticastDelegates
305                 /// </symmary>
306                 public static Delegate Combine (Delegate[] delegates)
307                 {
308                         if (delegates == null)
309                                 return null;
310
311                         Delegate retval = null;
312
313                         foreach (Delegate next in delegates)
314                                 retval = Combine (retval, next);
315
316                         return retval;
317                 }
318
319                 protected virtual Delegate CombineImpl (Delegate d)
320                 {
321                         throw new MulticastNotSupportedException ("");
322                 }
323
324                 public static Delegate Remove (Delegate source, Delegate value) 
325                 {
326                         if (source == null)
327                                 return null;
328
329                         return source.RemoveImpl (value);
330                 }
331
332                 protected virtual Delegate RemoveImpl (Delegate d)
333                 {
334                         if (this.Equals (d))
335                                 return null;
336
337                         return this;
338                 }
339 #if NET_1_1
340                 public static Delegate RemoveAll (Delegate source, Delegate value)
341                 {
342                         Delegate tmp = source;
343                         while ((source = Delegate.Remove (source, value)) != tmp)
344                                 tmp = source;
345
346                         return tmp;
347                 }
348 #endif
349                 public static bool operator == (Delegate a, Delegate b)
350                 {
351                         if ((object)a == null) {
352                                 if ((object)b == null)
353                                         return true;
354                                 return false;
355                         } else if ((object) b == null)
356                                 return false;
357                         
358                         return a.Equals (b);
359                 }
360
361                 public static bool operator != (Delegate a, Delegate b)
362                 {
363                         return !(a == b);
364                 }
365         }
366 }