2002-03-28 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mcs / class / corlib / System / Delegate.cs
1 //
2 // System.Delegate.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Daniel Stodden (stodden@in.tum.de)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10 // TODO:  Mucho left to implement
11 //
12
13 using System;
14 using System.Globalization;
15 using System.Reflection;
16 using System.Runtime.Serialization;
17
18 namespace System {
19
20         [MonoTODO]
21         public abstract class Delegate : ICloneable, ISerializable {
22                 protected Type target_type;
23                 protected object m_target;
24                 protected string method_name;
25                 protected IntPtr method_ptr;
26                 protected IntPtr delegate_trampoline;
27                 protected MethodInfo method_info;
28
29                 protected Delegate (object target, string method)
30                 {
31                         if (target == null)
32                                 throw new ArgumentNullException (Locale.GetText ("Target object is null"));
33
34                         if (method == null)
35                                 throw new ArgumentNullException (Locale.GetText ("method name is null"));
36
37                         this.target_type = null;
38                         this.method_ptr = IntPtr.Zero;
39                         this.m_target = target;
40                         this.method_name = method;
41                 }
42
43                 protected Delegate (Type target_type, string method)
44                 {
45                         if (m_target == null)
46                                 throw new ArgumentNullException (Locale.GetText ("Target type is null"));
47
48                         if (method == null)
49                                 throw new ArgumentNullException (Locale.GetText ("method string is null"));
50
51                         this.target_type = target_type;
52                         this.method_ptr = IntPtr.Zero;
53                         this.m_target = null;
54                         this.method_name = method;
55                 }
56
57                 public MethodInfo Method {
58                         get {
59                                 return method_info;
60                         }
61                 }
62
63                 public object Target {
64                         get {
65                                 return m_target;
66                         }
67                 }
68
69                 //
70                 // Methods
71                 //
72
73                 public object DynamicInvoke( object[] args )
74                 {
75                         return DynamicInvokeImpl( args );
76                 }
77
78                 public virtual object DynamicInvokeImpl( object[] args )
79                 {
80                         return Method.Invoke( m_target, args );
81                 }
82
83                 public virtual object Clone()
84                 {
85                         return MemberwiseClone();
86                 }
87
88                 public override bool Equals (object o)
89                 {
90                         if ( o == null )
91                                 return false;
92                         
93                         if ( o.GetType() != this.GetType() )
94                                 return false;
95
96                         Delegate d = (Delegate) o;
97                         if ((d.target_type == target_type) &&
98                             (d.m_target == m_target) &&
99                             (d.method_name == method_name) &&
100                             (d.method_ptr == method_ptr))
101                                 return true;
102
103                         return false;
104                 }
105
106                 public override int GetHashCode ()
107                 {
108                         return (int)method_ptr;
109                 }
110
111                 // This is from ISerializable
112                 [MonoTODO]
113                 public void GetObjectData (SerializationInfo info, StreamingContext context)
114                 {
115                         // TODO: IMPLEMENT ME
116                 }
117
118                 public virtual Delegate[] GetInvocationList()
119                 {
120                         return new Delegate[] { this };
121                 }
122
123                 /// <symmary>
124                 ///   Returns a new MulticastDelegate holding the
125                 ///   concatenated invocation lists of MulticastDelegates a and b
126                 /// </symmary>
127                 public static Delegate Combine (Delegate a, Delegate b)
128                 {
129                         if (a == null){
130                                 if (b == null)
131                                         return null;
132                                 return b;
133                         } else 
134                                 if (b == null)
135                                         return a;
136
137                         if (a.GetType () != b.GetType ())
138                                 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types"));
139                         
140                         return a.CombineImpl (b);
141                 }
142
143                 /// <symmary>
144                 ///   Returns a new MulticastDelegate holding the
145                 ///   concatenated invocation lists of an Array of MulticastDelegates
146                 /// </symmary>
147                 public static Delegate Combine( Delegate[] delegates )
148                 {
149                         Delegate retval = null;
150
151                         foreach ( Delegate next in delegates ) {
152                                 retval = Combine( retval, next );
153                         }
154
155                         return retval;
156                 }
157
158
159                 protected virtual Delegate CombineImpl (Delegate d)
160                 {
161                         throw new MulticastNotSupportedException ("");
162                 }
163                 
164                 [MonoTODO]
165                 public static Delegate Remove( Delegate source, Delegate value) {
166                         if ( source == null )
167                                 return null;
168                                 
169                         if ( value == null )
170                                 return source;
171
172                         throw new NotImplementedException ();
173                 }
174
175                 [MonoTODO]
176                 protected virtual Delegate RemoveImpl(Delegate d)
177                 {
178                         throw new NotImplementedException();
179                 }
180
181                 public static bool operator ==( Delegate a, Delegate b )
182                 {
183                         if ((object)a == null) {
184                                 if ((object)b == null)
185                                         return true;
186                                 return false;
187                         }
188                         return a.Equals( b );
189                 }
190
191                 public static bool operator !=( Delegate a, Delegate b )
192                 {
193                         return !(a == b);
194                 }
195         }
196 }