EventBuilder.cs 5.3 KB

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