ParameterBuilder.cs 5.7 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/ParameterBuilder.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 (_ParameterBuilder))]
  41. #endif
  42. [ClassInterface (ClassInterfaceType.None)]
  43. public class ParameterBuilder : _ParameterBuilder {
  44. private MethodBase methodb; /* MethodBuilder, ConstructorBuilder or DynamicMethod */
  45. private string name;
  46. private CustomAttributeBuilder[] cattrs;
  47. private UnmanagedMarshal marshal_info;
  48. private ParameterAttributes attrs;
  49. private int position;
  50. private int table_idx;
  51. object def_value;
  52. internal ParameterBuilder (MethodBase mb, int pos, ParameterAttributes attributes, string strParamName) {
  53. name = strParamName;
  54. position = pos;
  55. attrs = attributes;
  56. methodb = mb;
  57. #if NET_2_0
  58. if (mb is DynamicMethod)
  59. table_idx = 0;
  60. else
  61. #endif
  62. table_idx = mb.get_next_table_index (this, 0x08, true);
  63. }
  64. public virtual int Attributes {
  65. get {return (int)attrs;}
  66. }
  67. public bool IsIn {
  68. get {return ((int)attrs & (int)ParameterAttributes.In) != 0;}
  69. }
  70. public bool IsOut {
  71. get {return ((int)attrs & (int)ParameterAttributes.Out) != 0;}
  72. }
  73. public bool IsOptional {
  74. get {return ((int)attrs & (int)ParameterAttributes.Optional) != 0;}
  75. }
  76. public virtual string Name {
  77. get {return name;}
  78. }
  79. public virtual int Position {
  80. get {return position;}
  81. }
  82. public virtual ParameterToken GetToken() {
  83. return new ParameterToken (0x08 | table_idx);
  84. }
  85. public virtual void SetConstant (object defaultValue)
  86. {
  87. def_value = defaultValue;
  88. attrs |= ParameterAttributes.HasDefault;
  89. }
  90. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  91. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  92. if (attrname == "System.Runtime.InteropServices.InAttribute") {
  93. attrs |= ParameterAttributes.In;
  94. return;
  95. } else if (attrname == "System.Runtime.InteropServices.OutAttribute") {
  96. attrs |= ParameterAttributes.Out;
  97. return;
  98. } else if (attrname == "System.Runtime.InteropServices.OptionalAttribute") {
  99. attrs |= ParameterAttributes.Optional;
  100. return;
  101. } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
  102. attrs |= ParameterAttributes.HasFieldMarshal;
  103. marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
  104. /* FIXME: check for errors */
  105. return;
  106. } else if (attrname == "System.Runtime.InteropServices.DefaultParameterValueAttribute") {
  107. /* MS.NET doesn't handle this attribute but we handle it for consistency */
  108. CustomAttributeBuilder.CustomAttributeInfo cinfo = CustomAttributeBuilder.decode_cattr (customBuilder);
  109. /* FIXME: check for type compatibility */
  110. SetConstant (cinfo.ctorArgs [0]);
  111. return;
  112. }
  113. if (cattrs != null) {
  114. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  115. cattrs.CopyTo (new_array, 0);
  116. new_array [cattrs.Length] = customBuilder;
  117. cattrs = new_array;
  118. } else {
  119. cattrs = new CustomAttributeBuilder [1];
  120. cattrs [0] = customBuilder;
  121. }
  122. }
  123. #if NET_2_0
  124. [ComVisible (true)]
  125. #endif
  126. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  127. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  128. }
  129. #if NET_2_0
  130. [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
  131. #endif
  132. public virtual void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
  133. marshal_info = unmanagedMarshal;
  134. attrs |= ParameterAttributes.HasFieldMarshal;
  135. }
  136. void _ParameterBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  137. {
  138. throw new NotImplementedException ();
  139. }
  140. void _ParameterBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  141. {
  142. throw new NotImplementedException ();
  143. }
  144. void _ParameterBuilder.GetTypeInfoCount (out uint pcTInfo)
  145. {
  146. throw new NotImplementedException ();
  147. }
  148. void _ParameterBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. }
  153. }