EventBuilder.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. [ComDefaultInterface (typeof (_EventBuilder))]
  41. #endif
  42. [ClassInterface (ClassInterfaceType.None)]
  43. public sealed class EventBuilder : _EventBuilder {
  44. #pragma warning disable 169, 414
  45. string name;
  46. Type type;
  47. TypeBuilder typeb;
  48. CustomAttributeBuilder[] cattrs;
  49. MethodBuilder add_method;
  50. MethodBuilder remove_method;
  51. MethodBuilder raise_method;
  52. MethodBuilder[] other_methods;
  53. EventAttributes attrs;
  54. int table_idx;
  55. #pragma warning restore 169, 414
  56. internal EventBuilder (TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) {
  57. name = eventName;
  58. attrs = eventAttrs;
  59. type = eventType;
  60. typeb = tb;
  61. table_idx = get_next_table_index (this, 0x14, true);
  62. }
  63. internal int get_next_table_index (object obj, int table, bool inc) {
  64. return typeb.get_next_table_index (obj, table, inc);
  65. }
  66. public void AddOtherMethod( MethodBuilder mdBuilder) {
  67. if (mdBuilder == null)
  68. throw new ArgumentNullException ("mdBuilder");
  69. RejectIfCreated ();
  70. if (other_methods != null) {
  71. MethodBuilder[] newv = new MethodBuilder [other_methods.Length + 1];
  72. other_methods.CopyTo (newv, 0);
  73. other_methods = newv;
  74. } else {
  75. other_methods = new MethodBuilder [1];
  76. }
  77. other_methods [other_methods.Length - 1] = mdBuilder;
  78. }
  79. public EventToken GetEventToken () {
  80. return new EventToken (0x14000000 | table_idx);
  81. }
  82. public void SetAddOnMethod( MethodBuilder mdBuilder) {
  83. if (mdBuilder == null)
  84. throw new ArgumentNullException ("mdBuilder");
  85. RejectIfCreated ();
  86. add_method = mdBuilder;
  87. }
  88. public void SetRaiseMethod( MethodBuilder mdBuilder) {
  89. if (mdBuilder == null)
  90. throw new ArgumentNullException ("mdBuilder");
  91. RejectIfCreated ();
  92. raise_method = mdBuilder;
  93. }
  94. public void SetRemoveOnMethod( MethodBuilder mdBuilder) {
  95. if (mdBuilder == null)
  96. throw new ArgumentNullException ("mdBuilder");
  97. RejectIfCreated ();
  98. remove_method = mdBuilder;
  99. }
  100. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  101. if (customBuilder == null)
  102. throw new ArgumentNullException ("customBuilder");
  103. RejectIfCreated ();
  104. #if NET_2_0
  105. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  106. if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
  107. attrs |= EventAttributes.SpecialName;
  108. return;
  109. }
  110. #endif
  111. if (cattrs != null) {
  112. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  113. cattrs.CopyTo (new_array, 0);
  114. new_array [cattrs.Length] = customBuilder;
  115. cattrs = new_array;
  116. } else {
  117. cattrs = new CustomAttributeBuilder [1];
  118. cattrs [0] = customBuilder;
  119. }
  120. }
  121. #if NET_2_0
  122. [ComVisible (true)]
  123. #endif
  124. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  125. if (con == null)
  126. throw new ArgumentNullException ("con");
  127. if (binaryAttribute == null)
  128. throw new ArgumentNullException ("binaryAttribute");
  129. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  130. }
  131. private void RejectIfCreated () {
  132. if (typeb.is_created)
  133. throw new InvalidOperationException ("Type definition of the method is complete.");
  134. }
  135. void _EventBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  136. {
  137. throw new NotImplementedException ();
  138. }
  139. void _EventBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  140. {
  141. throw new NotImplementedException ();
  142. }
  143. void _EventBuilder.GetTypeInfoCount (out uint pcTInfo)
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. void _EventBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  148. {
  149. throw new NotImplementedException ();
  150. }
  151. }
  152. }