EventDefinition.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // EventDefinition.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using Mono.Collections.Generic;
  29. namespace Mono.Cecil {
  30. public sealed class EventDefinition : EventReference, IMemberDefinition {
  31. ushort attributes;
  32. Collection<CustomAttribute> custom_attributes;
  33. internal MethodDefinition add_method;
  34. internal MethodDefinition invoke_method;
  35. internal MethodDefinition remove_method;
  36. internal Collection<MethodDefinition> other_methods;
  37. public EventAttributes Attributes {
  38. get { return (EventAttributes) attributes; }
  39. set { attributes = (ushort) value; }
  40. }
  41. public MethodDefinition AddMethod {
  42. get {
  43. if (add_method != null)
  44. return add_method;
  45. InitializeMethods ();
  46. return add_method;
  47. }
  48. set { add_method = value; }
  49. }
  50. public MethodDefinition InvokeMethod {
  51. get {
  52. if (invoke_method != null)
  53. return invoke_method;
  54. InitializeMethods ();
  55. return invoke_method;
  56. }
  57. set { invoke_method = value; }
  58. }
  59. public MethodDefinition RemoveMethod {
  60. get {
  61. if (remove_method != null)
  62. return remove_method;
  63. InitializeMethods ();
  64. return remove_method;
  65. }
  66. set { remove_method = value; }
  67. }
  68. public bool HasOtherMethods {
  69. get {
  70. if (other_methods != null)
  71. return other_methods.Count > 0;
  72. InitializeMethods ();
  73. return !Mixin.IsNullOrEmpty (other_methods);
  74. }
  75. }
  76. public Collection<MethodDefinition> OtherMethods {
  77. get {
  78. if (other_methods != null)
  79. return other_methods;
  80. InitializeMethods ();
  81. if (other_methods != null)
  82. return other_methods;
  83. return other_methods = new Collection<MethodDefinition> ();
  84. }
  85. }
  86. public bool HasCustomAttributes {
  87. get {
  88. if (custom_attributes != null)
  89. return custom_attributes.Count > 0;
  90. return Mixin.GetHasCustomAttributes(this, Module);
  91. }
  92. }
  93. public Collection<CustomAttribute> CustomAttributes {
  94. get { return custom_attributes ?? (Mixin.GetCustomAttributes(this, ref custom_attributes, Module)); }
  95. }
  96. #region EventAttributes
  97. public bool IsSpecialName {
  98. get { return Mixin.GetAttributes(attributes,(ushort) EventAttributes.SpecialName); }
  99. set { attributes =Mixin.SetAttributes(attributes,(ushort) EventAttributes.SpecialName, value); }
  100. }
  101. public bool IsRuntimeSpecialName {
  102. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.RTSpecialName); }
  103. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.RTSpecialName, value); }
  104. }
  105. #endregion
  106. public new TypeDefinition DeclaringType {
  107. get { return (TypeDefinition) base.DeclaringType; }
  108. set { base.DeclaringType = value; }
  109. }
  110. public override bool IsDefinition {
  111. get { return true; }
  112. }
  113. public EventDefinition (string name, EventAttributes attributes, TypeReference eventType)
  114. : base (name, eventType)
  115. {
  116. this.attributes = (ushort) attributes;
  117. this.token = new MetadataToken (TokenType.Event);
  118. }
  119. void InitializeMethods ()
  120. {
  121. var module = this.Module;
  122. lock (module.SyncRoot) {
  123. if (add_method != null
  124. || invoke_method != null
  125. || remove_method != null)
  126. return;
  127. if (!Mixin.HasImage(module))
  128. return;
  129. module.Read (this, (@event, reader) => reader.ReadMethods (@event));
  130. }
  131. }
  132. public override EventDefinition Resolve ()
  133. {
  134. return this;
  135. }
  136. }
  137. }