EventBuilder.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. // System.Reflection.Emit/EventBuilder.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001 Ximian, Inc. http://www.ximian.com
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection.Emit {
  38. #if NET_2_0
  39. [ComVisible (true)]
  40. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  41. [ComDefaultInterfaceAttribute (typeof (_EventBuilder))]
  42. #endif
  43. public sealed class EventBuilder {
  44. string name;
  45. Type type;
  46. TypeBuilder typeb;
  47. CustomAttributeBuilder[] cattrs;
  48. MethodBuilder add_method;
  49. MethodBuilder remove_method;
  50. MethodBuilder raise_method;
  51. MethodBuilder[] other_methods;
  52. EventAttributes attrs;
  53. int table_idx;
  54. internal EventBuilder (TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) {
  55. name = eventName;
  56. attrs = eventAttrs;
  57. type = eventType;
  58. typeb = tb;
  59. table_idx = get_next_table_index (this, 0x14, true);
  60. }
  61. internal int get_next_table_index (object obj, int table, bool inc) {
  62. return typeb.get_next_table_index (obj, table, inc);
  63. }
  64. public void AddOtherMethod( MethodBuilder mdBuilder) {
  65. if (mdBuilder == null)
  66. throw new ArgumentNullException ("mdBuilder");
  67. RejectIfCreated ();
  68. if (other_methods != null) {
  69. MethodBuilder[] newv = new MethodBuilder [other_methods.Length + 1];
  70. other_methods.CopyTo (newv, 0);
  71. other_methods = newv;
  72. } else {
  73. other_methods = new MethodBuilder [1];
  74. }
  75. other_methods [other_methods.Length - 1] = mdBuilder;
  76. }
  77. public EventToken GetEventToken () {
  78. return new EventToken (0x14000000 | table_idx);
  79. }
  80. public void SetAddOnMethod( MethodBuilder mdBuilder) {
  81. if (mdBuilder == null)
  82. throw new ArgumentNullException ("mdBuilder");
  83. RejectIfCreated ();
  84. add_method = mdBuilder;
  85. }
  86. public void SetRaiseMethod( MethodBuilder mdBuilder) {
  87. if (mdBuilder == null)
  88. throw new ArgumentNullException ("mdBuilder");
  89. RejectIfCreated ();
  90. raise_method = mdBuilder;
  91. }
  92. public void SetRemoveOnMethod( MethodBuilder mdBuilder) {
  93. if (mdBuilder == null)
  94. throw new ArgumentNullException ("mdBuilder");
  95. RejectIfCreated ();
  96. remove_method = mdBuilder;
  97. }
  98. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  99. if (customBuilder == null)
  100. throw new ArgumentNullException ("customBuilder");
  101. RejectIfCreated ();
  102. if (cattrs != null) {
  103. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  104. cattrs.CopyTo (new_array, 0);
  105. new_array [cattrs.Length] = customBuilder;
  106. cattrs = new_array;
  107. } else {
  108. cattrs = new CustomAttributeBuilder [1];
  109. cattrs [0] = customBuilder;
  110. }
  111. }
  112. #if NET_2_0
  113. [ComVisible (true)]
  114. #endif
  115. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  116. if (con == null)
  117. throw new ArgumentNullException ("con");
  118. if (binaryAttribute == null)
  119. throw new ArgumentNullException ("binaryAttribute");
  120. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  121. }
  122. private void RejectIfCreated () {
  123. if (typeb.is_created)
  124. throw new InvalidOperationException ("Type definition of the method is complete.");
  125. }
  126. }
  127. }