MonoEvent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Reflection;
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.InteropServices;
  35. using System.Runtime.Serialization;
  36. namespace System.Reflection {
  37. internal struct MonoEventInfo {
  38. public Type declaring_type;
  39. public Type reflected_type;
  40. public String name;
  41. public MethodInfo add_method;
  42. public MethodInfo remove_method;
  43. public MethodInfo raise_method;
  44. public EventAttributes attrs;
  45. public MethodInfo[] other_methods;
  46. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  47. static extern void get_event_info (MonoEvent ev, out MonoEventInfo info);
  48. internal static MonoEventInfo GetEventInfo (MonoEvent ev)
  49. {
  50. MonoEventInfo mei;
  51. MonoEventInfo.get_event_info (ev, out mei);
  52. return mei;
  53. }
  54. }
  55. [Serializable]
  56. internal sealed class MonoEvent: EventInfo, ISerializable
  57. {
  58. #pragma warning disable 169
  59. IntPtr klass;
  60. IntPtr handle;
  61. #pragma warning restore 169
  62. public override EventAttributes Attributes {
  63. get {
  64. return MonoEventInfo.GetEventInfo (this).attrs;
  65. }
  66. }
  67. public override MethodInfo GetAddMethod (bool nonPublic)
  68. {
  69. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  70. if (nonPublic || (info.add_method != null && info.add_method.IsPublic))
  71. return info.add_method;
  72. return null;
  73. }
  74. public override MethodInfo GetRaiseMethod (bool nonPublic)
  75. {
  76. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  77. if (nonPublic || (info.raise_method != null && info.raise_method.IsPublic))
  78. return info.raise_method;
  79. return null;
  80. }
  81. public override MethodInfo GetRemoveMethod (bool nonPublic)
  82. {
  83. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  84. if (nonPublic || (info.remove_method != null && info.remove_method.IsPublic))
  85. return info.remove_method;
  86. return null;
  87. }
  88. public override MethodInfo[] GetOtherMethods (bool nonPublic)
  89. {
  90. MonoEventInfo info = MonoEventInfo.GetEventInfo (this);
  91. if (nonPublic)
  92. return info.other_methods;
  93. int num_public = 0;
  94. foreach (MethodInfo m in info.other_methods) {
  95. if (m.IsPublic)
  96. num_public++;
  97. }
  98. if (num_public == info.other_methods.Length)
  99. return info.other_methods;
  100. MethodInfo[] res = new MethodInfo [num_public];
  101. num_public = 0;
  102. foreach (MethodInfo m in info.other_methods) {
  103. if (m.IsPublic)
  104. res [num_public++] = m;
  105. }
  106. return res;
  107. }
  108. public override Type DeclaringType {
  109. get {
  110. return MonoEventInfo.GetEventInfo (this).declaring_type;
  111. }
  112. }
  113. public override Type ReflectedType {
  114. get {
  115. return MonoEventInfo.GetEventInfo (this).reflected_type;
  116. }
  117. }
  118. public override string Name {
  119. get {
  120. return MonoEventInfo.GetEventInfo (this).name;
  121. }
  122. }
  123. public override string ToString ()
  124. {
  125. return EventHandlerType + " " + Name;
  126. }
  127. public override bool IsDefined (Type attributeType, bool inherit)
  128. {
  129. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  130. }
  131. public override object[] GetCustomAttributes( bool inherit)
  132. {
  133. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  134. }
  135. public override object[] GetCustomAttributes( Type attributeType, bool inherit)
  136. {
  137. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  138. }
  139. // ISerializable
  140. public void GetObjectData (SerializationInfo info, StreamingContext context)
  141. {
  142. MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
  143. ToString(), MemberTypes.Event);
  144. }
  145. }
  146. }