2 // System.Reflection/EventInfo.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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 {
37 [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
40 [ClassInterface(ClassInterfaceType.None)]
41 public abstract class EventInfo : MemberInfo, _EventInfo {
43 AddEventAdapter cached_add_event;
48 public abstract EventAttributes Attributes {get;}
50 public Type EventHandlerType {
53 MethodInfo add = GetAddMethod (true);
54 p = add.GetParameters ();
56 Type t = p [0].ParameterType;
57 /* is it alwasys the first arg?
58 if (!t.IsSubclassOf (typeof (System.Delegate)))
59 throw new Exception ("no delegate in event");*/
65 public bool IsMulticast {get {return true;}}
66 public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
67 public override MemberTypes MemberType {
68 get {return MemberTypes.Event;}
71 protected EventInfo() {
75 public new Type GetType ()
77 return base.GetType ();
83 public void AddEventHandler (object target, Delegate handler)
86 if (cached_add_event == null) {
87 MethodInfo add = GetAddMethod ();
89 throw new Exception ("No add method!?");
90 if (add.DeclaringType.IsValueType) {
91 add.Invoke (target, new object [] {handler});
94 cached_add_event = CreateAddEventDelegate (add);
96 cached_add_event (target, handler);
98 MethodInfo add = GetAddMethod ();
100 throw new Exception ("No add method!?");
101 add.Invoke (target, new object [] {handler});
105 public MethodInfo GetAddMethod() {
106 return GetAddMethod (false);
108 public abstract MethodInfo GetAddMethod(bool nonPublic);
109 public MethodInfo GetRaiseMethod() {
110 return GetRaiseMethod (false);
112 public abstract MethodInfo GetRaiseMethod( bool nonPublic);
113 public MethodInfo GetRemoveMethod() {
114 return GetRemoveMethod (false);
116 public abstract MethodInfo GetRemoveMethod( bool nonPublic);
119 public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
120 // implemented by the derived class
121 return new MethodInfo [0];
124 public MethodInfo[] GetOtherMethods () {
125 return GetOtherMethods (false);
130 [DebuggerStepThrough]
131 public void RemoveEventHandler (object target, Delegate handler)
133 MethodInfo remove = GetRemoveMethod ();
135 throw new Exception ("No remove method!?");
137 remove.Invoke (target, new object [] {handler});
140 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
142 throw new NotImplementedException ();
145 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
147 throw new NotImplementedException ();
150 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
152 throw new NotImplementedException ();
155 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
157 throw new NotImplementedException ();
160 delegate void AddEventAdapter (object _this, Delegate dele);
161 delegate void AddEvent<T, D> (T _this, D dele);
162 delegate void StaticAddEvent<D> (D dele);
164 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
166 addEvent ((T)obj, (D)dele);
169 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
175 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
176 * The first delegate performs casting and boxing, the second dispatch to the add_... method.
178 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
182 object addHandlerDelegate;
183 MethodInfo adapterFrame;
184 Type addHandlerDelegateType;
187 if (method.IsStatic) {
188 typeVector = new Type[] { method.GetParameters () [0].ParameterType };
189 addHandlerDelegateType = typeof (StaticAddEvent<>);
190 frameName = "StaticAddEventAdapterFrame";
192 typeVector = new Type[] { method.DeclaringType, method.GetParameters () [0].ParameterType };
193 addHandlerDelegateType = typeof (AddEvent<,>);
194 frameName = "AddEventFrame";
197 addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
199 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
200 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
201 // delegate that we can transform into a MethodAccessException
202 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
203 if (addHandlerDelegate == null)
204 throw new MethodAccessException ();
206 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
208 adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
209 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
210 return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);