ParameterBuilder.cs 5.8 KB

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