2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Reflection / EventInfo.cs
blob9137379f4c5cc77d33aee396a52cbff0d8472c52
1 //
2 // System.Reflection/EventInfo.cs
3 //
4 // Author:
5 // Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Diagnostics;
31 using System.Runtime.InteropServices;
33 namespace System.Reflection {
35 [ComVisible (true)]
36 [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
37 [Serializable]
38 [ClassInterface(ClassInterfaceType.None)]
39 public abstract class EventInfo : MemberInfo, _EventInfo {
40 AddEventAdapter cached_add_event;
42 public abstract EventAttributes Attributes {get;}
44 public
45 #if NET_4_0
46 virtual
47 #endif
48 Type EventHandlerType {
49 get {
50 ParameterInfo[] p;
51 MethodInfo add = GetAddMethod (true);
52 p = add.GetParameters ();
53 if (p.Length > 0) {
54 Type t = p [0].ParameterType;
55 /* is it alwasys the first arg?
56 if (!t.IsSubclassOf (typeof (System.Delegate)))
57 throw new Exception ("no delegate in event");*/
58 return t;
59 } else
60 return null;
64 public
65 #if NET_4_0
66 virtual
67 #endif
68 bool IsMulticast {get {return true;}}
69 public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
70 public override MemberTypes MemberType {
71 get {return MemberTypes.Event;}
74 protected EventInfo() {
78 [DebuggerHidden]
79 [DebuggerStepThrough]
80 public
81 #if NET_4_0
82 virtual
83 #endif
84 void AddEventHandler (object target, Delegate handler)
86 if (cached_add_event == null) {
87 MethodInfo add = GetAddMethod ();
88 if (add == null)
89 throw new InvalidOperationException ("Cannot add a handler to an event that doesn't have a visible add method");
90 if (add.DeclaringType.IsValueType) {
91 if (target == null && !add.IsStatic)
92 throw new TargetException ("Cannot add a handler to a non static event with a null target");
93 add.Invoke (target, new object [] {handler});
94 return;
96 cached_add_event = CreateAddEventDelegate (add);
98 //if (target == null && is_instance)
99 // throw new TargetException ("Cannot add a handler to a non static event with a null target");
100 cached_add_event (target, handler);
103 public MethodInfo GetAddMethod() {
104 return GetAddMethod (false);
106 public abstract MethodInfo GetAddMethod(bool nonPublic);
107 public MethodInfo GetRaiseMethod() {
108 return GetRaiseMethod (false);
110 public abstract MethodInfo GetRaiseMethod( bool nonPublic);
111 public MethodInfo GetRemoveMethod() {
112 return GetRemoveMethod (false);
114 public abstract MethodInfo GetRemoveMethod( bool nonPublic);
116 public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
117 // implemented by the derived class
118 return new MethodInfo [0];
121 public MethodInfo[] GetOtherMethods () {
122 return GetOtherMethods (false);
125 [DebuggerHidden]
126 [DebuggerStepThrough]
127 public
128 #if NET_4_0
129 virtual
130 #endif
131 void RemoveEventHandler (object target, Delegate handler)
133 MethodInfo remove = GetRemoveMethod ();
134 if (remove == null)
135 throw new InvalidOperationException ("Cannot remove a handler to an event that doesn't have a visible remove method");
137 remove.Invoke (target, new object [] {handler});
140 #if NET_4_0
141 public override bool Equals (object obj)
143 return obj == this;
146 public override int GetHashCode ()
148 return base.GetHashCode ();
151 public static bool operator == (EventInfo left, EventInfo right)
153 if ((object)left == (object)right)
154 return true;
155 if ((object)left == null ^ (object)right == null)
156 return false;
157 return left.Equals (right);
160 public static bool operator != (EventInfo left, EventInfo right)
162 if ((object)left == (object)right)
163 return false;
164 if ((object)left == null ^ (object)right == null)
165 return true;
166 return !left.Equals (right);
168 #endif
170 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
172 throw new NotImplementedException ();
175 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
177 throw new NotImplementedException ();
180 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
182 throw new NotImplementedException ();
185 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
187 throw new NotImplementedException ();
189 delegate void AddEventAdapter (object _this, Delegate dele);
190 delegate void AddEvent<T, D> (T _this, D dele);
191 delegate void StaticAddEvent<D> (D dele);
193 #pragma warning disable 169
194 // Used via reflection
195 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
197 if (obj == null)
198 throw new TargetException ("Cannot add a handler to a non static event with a null target");
199 if (!(obj is T))
200 throw new TargetException ("Object doesn't match target");
201 addEvent ((T)obj, (D)dele);
204 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
206 addEvent ((D)dele);
208 #pragma warning restore 169
211 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
212 * The first delegate performs casting and boxing, the second dispatch to the add_... method.
214 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
216 Type[] typeVector;
217 Type addHandlerType;
218 object addHandlerDelegate;
219 MethodInfo adapterFrame;
220 Type addHandlerDelegateType;
221 string frameName;
223 if (method.IsStatic) {
224 typeVector = new Type[] { method.GetParameters () [0].ParameterType };
225 addHandlerDelegateType = typeof (StaticAddEvent<>);
226 frameName = "StaticAddEventAdapterFrame";
227 } else {
228 typeVector = new Type[] { method.DeclaringType, method.GetParameters () [0].ParameterType };
229 addHandlerDelegateType = typeof (AddEvent<,>);
230 frameName = "AddEventFrame";
233 addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
234 #if NET_2_1
235 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
236 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
237 // delegate that we can transform into a MethodAccessException
238 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
239 if (addHandlerDelegate == null)
240 throw new MethodAccessException ();
241 #else
242 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
243 #endif
244 adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
245 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
246 return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);