Wed Nov 14 16:30:27 CET 2001 Paolo Molaro <lupus@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 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9 // TODO:  Mucho left to implement
10 //
11
12 using System;
13 using System.Runtime.Serialization;
14
15 namespace System {
16
17         public abstract class Delegate : ICloneable, ISerializable {
18                 protected Type target_type;
19                 protected object m_target;
20                 protected string method;
21                 protected IntPtr method_ptr;
22
23                 protected Delegate (object target, string method)
24                 {
25                         if (target == null)
26                                 throw new ArgumentNullException ("Target object is null");
27
28                         if (method == null)
29                                 throw new ArgumentNullException ("method name is null");
30
31                         this.target_type = null;
32                         this.method_ptr = IntPtr.Zero;
33                         this.m_target = target;
34                         this.method = method;
35                 }
36
37                 protected Delegate (Type target_type, string method)
38                 {
39                         if (m_target == null)
40                                 throw new ArgumentNullException ("Target type is null");
41
42                         if (method == null)
43                                 throw new ArgumentNullException ("method string is null");
44
45                         this.target_type = target_type;
46                         this.method_ptr = IntPtr.Zero;
47                         this.m_target = null;
48                         this.method = method;
49                 }
50
51 #if NOTYET
52                 public MethodInfo Method {
53                         get {
54                                 return null;
55                         }
56                 }
57 #endif
58
59                 public object Target {
60                         get {
61                                 return m_target;
62                         }
63                 }
64
65
66                 //
67                 // Methods
68                 //
69
70                 public abstract object Clone ();
71
72                 public override bool Equals (object o)
73                 {
74                         if (!(o is System.Delegate))
75                                 return false;
76
77                         Delegate d = (Delegate) o;
78                         
79                         if ((d.target_type == target_type) &&
80                             (d.m_target == m_target) &&
81                             (d.method == method))
82                                 return true;
83
84                         return false;
85                 }
86
87                 public override int GetHashCode ()
88                 {
89                         return method.GetHashCode ();
90                 }
91
92                 // This is from ISerializable
93                 public void GetObjectData (SerializationInfo info, StreamingContext context)
94                 {
95                         // TODO: IMPLEMENT ME
96                 }
97
98                 public static Delegate Combine (Delegate a, Delegate b)
99                 {
100                         if (a == null){
101                                 if (b == null)
102                                         return null;
103                                 return b;
104                         } else 
105                                 if (b == null)
106                                         return a;
107
108                         if (a.GetType () != b.GetType ())
109                                 throw new ArgumentException ("Incompatible Delegate Types");
110                         
111                         return a.CombineImpl (b);
112                 }
113
114                 protected virtual Delegate CombineImpl (Delegate d)
115                 {
116                         throw new MulticastNotSupportedException ("");
117                 }
118         }
119 }