MonoEvent.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // System.Reflection/MonoEvent.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004, 2009 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Reflection;
  34. using System.Runtime.CompilerServices;
  35. using System.Runtime.InteropServices;
  36. using System.Runtime.Serialization;
  37. namespace System.Reflection {
  38. internal struct MonoEventInfo {
  39. public Type declaring_type;
  40. public Type reflected_type;
  41. public String name;
  42. public MethodInfo add_method;
  43. public MethodInfo remove_method;
  44. public MethodInfo raise_method;
  45. public EventAttributes attrs;
  46. public MethodInfo[] other_methods;
  47. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  48. static extern void get_event_info (MonoEvent ev, out MonoEventInfo info);
  49. internal static MonoEventInfo GetEventInfo (MonoEvent ev)
  50. {
  51. MonoEventInfo mei;
  52. MonoEventInfo.get_event_info (ev, out mei);
  53. return mei;
  54. }
  55. }
  56. [Serializable]
  57. internal sealed class MonoEvent: EventInfo, ISerializable
  58. {
  59. #pragma warning disable 169
  60. IntPtr klass;
  61. IntPtr handle;
  62. #pragma warning restore 169
  63. public override EventAttributes Attributes {
  64. get {
  65. return MonoEventInfo.GetEventInfo (this).attrs;
  66. }
  67. }
  68. public override MethodInfo GetAddMethod (bool nonPublic)
  69. {
  70. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  71. if (nonPublic || (info.add_method != null && info.add_method.IsPublic))
  72. return info.add_method;
  73. return null;
  74. }
  75. public override MethodInfo GetRaiseMethod (bool nonPublic)
  76. {
  77. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  78. if (nonPublic || (info.raise_method != null && info.raise_method.IsPublic))
  79. return info.raise_method;
  80. return null;
  81. }
  82. public override MethodInfo GetRemoveMethod (bool nonPublic)
  83. {
  84. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  85. if (nonPublic || (info.remove_method != null && info.remove_method.IsPublic))
  86. return info.remove_method;
  87. return null;
  88. }
  89. public override MethodInfo[] GetOtherMethods (bool nonPublic)
  90. {
  91. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  92. if (nonPublic)
  93. return info.other_methods;
  94. int num_public = 0;
  95. foreach (MethodInfo m in info.other_methods) {
  96. if (m.IsPublic)
  97. num_public++;
  98. }
  99. if (num_public == info.other_methods.Length)
  100. return info.other_methods;
  101. MethodInfo[] res = new MethodInfo [num_public];
  102. num_public = 0;
  103. foreach (MethodInfo m in info.other_methods) {
  104. if (m.IsPublic)
  105. res [num_public++] = m;
  106. }
  107. return res;
  108. }
  109. public override Type DeclaringType {
  110. get {
  111. return MonoEventInfo.GetEventInfo (this).declaring_type;
  112. }
  113. }
  114. public override Type ReflectedType {
  115. get {
  116. return MonoEventInfo.GetEventInfo (this).reflected_type;
  117. }
  118. }
  119. public override string Name {
  120. get {
  121. return MonoEventInfo.GetEventInfo (this).name;
  122. }
  123. }
  124. public override string ToString ()
  125. {
  126. return EventHandlerType + " " + Name;
  127. }
  128. public override bool IsDefined (Type attributeType, bool inherit)
  129. {
  130. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  131. }
  132. public override object[] GetCustomAttributes( bool inherit)
  133. {
  134. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  135. }
  136. public override object[] GetCustomAttributes( Type attributeType, bool inherit)
  137. {
  138. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  139. }
  140. // ISerializable
  141. public void GetObjectData (SerializationInfo info, StreamingContext context)
  142. {
  143. MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
  144. ToString(), MemberTypes.Event);
  145. }
  146. #if NET_4_0
  147. public override IList<CustomAttributeData> GetCustomAttributesData () {
  148. return CustomAttributeData.GetCustomAttributes (this);
  149. }
  150. #endif
  151. }
  152. }