Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Delegate.cs « System « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c332cd8cf42197dc284b6542acbb389ff98b93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// System.Delegate.cs
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//   Daniel Stodden (stodden@in.tum.de)
//
// (C) Ximian, Inc.  http://www.ximian.com
//
// TODO:  Mucho left to implement
//

using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;

namespace System {

	[MonoTODO]
	public abstract class Delegate : ICloneable, ISerializable {
		protected Type target_type;
		protected object m_target;
		protected string method_name;
		protected IntPtr method_ptr;
		protected IntPtr delegate_trampoline;
		protected MethodInfo method_info;

		protected Delegate (object target, string method)
		{
			if (target == null)
				throw new ArgumentNullException (Locale.GetText ("Target object is null"));

			if (method == null)
				throw new ArgumentNullException (Locale.GetText ("method name is null"));

			this.target_type = null;
			this.method_ptr = IntPtr.Zero;
			this.m_target = target;
			this.method_name = method;
		}

		protected Delegate (Type target_type, string method)
		{
			if (m_target == null)
				throw new ArgumentNullException (Locale.GetText ("Target type is null"));

			if (method == null)
				throw new ArgumentNullException (Locale.GetText ("method string is null"));

			this.target_type = target_type;
			this.method_ptr = IntPtr.Zero;
			this.m_target = null;
			this.method_name = method;
		}

		public MethodInfo Method {
			get {
				return method_info;
			}
		}

		public object Target {
			get {
				return m_target;
			}
		}

		//
		// Methods
		//

		public object DynamicInvoke( object[] args )
		{
			return DynamicInvokeImpl( args );
		}

		public virtual object DynamicInvokeImpl( object[] args )
		{
			return Method.Invoke( m_target, args );
		}

		public virtual object Clone()
		{
			return MemberwiseClone();
		}

		public override bool Equals (object o)
		{
			if ( o == null )
				return false;
			
			if ( o.GetType() != this.GetType() )
				return false;

			Delegate d = (Delegate) o;
			if ((d.target_type == target_type) &&
			    (d.m_target == m_target) &&
			    (d.method_name == method_name) &&
			    (d.method_ptr == method_ptr))
				return true;

			return false;
		}

		public override int GetHashCode ()
		{
			return (int)method_ptr;
		}

		// This is from ISerializable
		[MonoTODO]
		public void GetObjectData (SerializationInfo info, StreamingContext context)
		{
			// TODO: IMPLEMENT ME
		}

		public virtual Delegate[] GetInvocationList()
		{
			return new Delegate[] { this };
		}

		/// <symmary>
		///   Returns a new MulticastDelegate holding the
		///   concatenated invocation lists of MulticastDelegates a and b
		/// </symmary>
		public static Delegate Combine (Delegate a, Delegate b)
		{
			if (a == null){
				if (b == null)
					return null;
				return b;
			} else 
				if (b == null)
					return a;

			if (a.GetType () != b.GetType ())
				throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types"));
			
			return a.CombineImpl (b);
		}

		/// <symmary>
		///   Returns a new MulticastDelegate holding the
		///   concatenated invocation lists of an Array of MulticastDelegates
		/// </symmary>
		public static Delegate Combine( Delegate[] delegates )
		{
			Delegate retval = null;

			foreach ( Delegate next in delegates )
				retval = Combine( retval, next );

			return retval;
		}


		protected virtual Delegate CombineImpl (Delegate d)
		{
			throw new MulticastNotSupportedException ("");
		}
		
		
		public static Delegate Remove( Delegate source, Delegate value ) 
		{
			if ( source == null )
				return null;
				
			return source.RemoveImpl( value );
		}

		protected virtual Delegate RemoveImpl( Delegate d )
		{
			if ( this.Equals( d ) )
				return null;
		       
			return this;
		}

		public static bool operator ==( Delegate a, Delegate b )
		{
			if ((object)a == null) {
				if ((object)b == null)
					return true;
				return false;
			}
			return a.Equals( b );
		}

		public static bool operator !=( Delegate a, Delegate b )
		{
			return !(a == b);
		}
	}
}