EventInfo.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // System.Reflection/EventInfo.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  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.
  28. //
  29. using System.Diagnostics;
  30. using System.Runtime.InteropServices;
  31. namespace System.Reflection {
  32. #if NET_2_0
  33. [ComVisible (true)]
  34. [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
  35. [Serializable]
  36. #endif
  37. [ClassInterface(ClassInterfaceType.None)]
  38. public abstract class EventInfo : MemberInfo, _EventInfo {
  39. public abstract EventAttributes Attributes {get;}
  40. public Type EventHandlerType {
  41. get {
  42. ParameterInfo[] p;
  43. MethodInfo add = GetAddMethod (true);
  44. p = add.GetParameters ();
  45. if (p.Length > 0) {
  46. Type t = p [0].ParameterType;
  47. /* is it alwasys the first arg?
  48. if (!t.IsSubclassOf (typeof (System.Delegate)))
  49. throw new Exception ("no delegate in event");*/
  50. return t;
  51. } else
  52. return null;
  53. }
  54. }
  55. public bool IsMulticast {get {return true;}}
  56. public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
  57. public override MemberTypes MemberType {
  58. get {return MemberTypes.Event;}
  59. }
  60. protected EventInfo() {
  61. }
  62. [DebuggerHidden]
  63. [DebuggerStepThrough]
  64. public void AddEventHandler (object target, Delegate handler)
  65. {
  66. MethodInfo add = GetAddMethod ();
  67. if (add == null)
  68. throw new Exception ("No add method!?");
  69. add.Invoke (target, new object [] {handler});
  70. }
  71. public MethodInfo GetAddMethod() {
  72. return GetAddMethod (false);
  73. }
  74. public abstract MethodInfo GetAddMethod(bool nonPublic);
  75. public MethodInfo GetRaiseMethod() {
  76. return GetRaiseMethod (false);
  77. }
  78. public abstract MethodInfo GetRaiseMethod( bool nonPublic);
  79. public MethodInfo GetRemoveMethod() {
  80. return GetRemoveMethod (false);
  81. }
  82. public abstract MethodInfo GetRemoveMethod( bool nonPublic);
  83. #if NET_2_0
  84. public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
  85. // Shouldn't this be abstract, like the other methods ?
  86. throw new NotImplementedException ();
  87. }
  88. public MethodInfo[] GetOtherMethods () {
  89. return GetOtherMethods (false);
  90. }
  91. #endif
  92. [DebuggerHidden]
  93. [DebuggerStepThrough]
  94. public void RemoveEventHandler (object target, Delegate handler)
  95. {
  96. MethodInfo remove = GetRemoveMethod ();
  97. if (remove == null)
  98. throw new Exception ("No remove method!?");
  99. remove.Invoke (target, new object [] {handler});
  100. }
  101. }
  102. }