ReflectionEventDescriptor.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // System.ComponentModel.EventDescriptor.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) Novell, Inc. 2004
  8. //
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using System.Collections;
  12. using System.Reflection;
  13. namespace System.ComponentModel
  14. {
  15. internal class ReflectionEventDescriptor: EventDescriptor
  16. {
  17. Hashtable handlers;
  18. Type _eventType;
  19. Type _componentType;
  20. EventInfo _eventInfo;
  21. public ReflectionEventDescriptor (EventInfo eventInfo) : base (eventInfo.Name, (Attribute[]) eventInfo.GetCustomAttributes (true))
  22. {
  23. _eventInfo = eventInfo;
  24. _componentType = eventInfo.DeclaringType;
  25. _eventType = eventInfo.EventHandlerType;
  26. }
  27. public ReflectionEventDescriptor (Type componentType, EventDescriptor oldEventDescriptor, Attribute[] attrs) : base (oldEventDescriptor, attrs)
  28. {
  29. _componentType = componentType;
  30. _eventType = oldEventDescriptor.EventType;
  31. }
  32. public ReflectionEventDescriptor (Type componentType, string name, Type type, Attribute[] attrs) : base (name, attrs)
  33. {
  34. _componentType = componentType;
  35. _eventType = type;
  36. }
  37. EventInfo GetEventInfo ()
  38. {
  39. if (_eventInfo == null) {
  40. _eventInfo = _componentType.GetEvent (Name);
  41. if (_eventInfo == null)
  42. throw new ArgumentException ("Accessor methods for the " + Name + " event are missing");
  43. }
  44. return _eventInfo;
  45. }
  46. public override void AddEventHandler (object component, System.Delegate value)
  47. {
  48. if (handlers == null)
  49. handlers = new Hashtable ();
  50. ArrayList delegates = (ArrayList) handlers [component];
  51. if (delegates == null) {
  52. delegates = new ArrayList ();
  53. handlers [component] = delegates;
  54. }
  55. if (!delegates.Contains (value))
  56. delegates.Add (value);
  57. }
  58. public override void RemoveEventHandler (object component, System.Delegate value)
  59. {
  60. if (handlers == null) return;
  61. ArrayList delegates = (ArrayList) handlers [component];
  62. if (delegates == null) return;
  63. delegates.Remove (value);
  64. }
  65. public override System.Type ComponentType
  66. {
  67. get { return _componentType; }
  68. }
  69. public override System.Type EventType
  70. {
  71. get { return _eventType; }
  72. }
  73. public override bool IsMulticast
  74. {
  75. get { return GetEventInfo().IsMulticast; }
  76. }
  77. }
  78. }