ParameterBuilder.cs 5.7 KB

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