2
0

ParameterBuilder.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  41. [ComDefaultInterfaceAttribute (typeof (_ParameterBuilder))]
  42. #endif
  43. public class ParameterBuilder {
  44. private MethodBase methodb; /* MethodBuilder or ConstructorBuilder */
  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. table_idx = mb.get_next_table_index (this, 0x08, true);
  58. }
  59. public virtual int Attributes {
  60. get {return (int)attrs;}
  61. }
  62. public bool IsIn {
  63. get {return ((int)attrs & (int)ParameterAttributes.In) != 0;}
  64. }
  65. public bool IsOut {
  66. get {return ((int)attrs & (int)ParameterAttributes.Out) != 0;}
  67. }
  68. public bool IsOptional {
  69. get {return ((int)attrs & (int)ParameterAttributes.Optional) != 0;}
  70. }
  71. public virtual string Name {
  72. get {return name;}
  73. }
  74. public virtual int Position {
  75. get {return position;}
  76. }
  77. public virtual ParameterToken GetToken() {
  78. return new ParameterToken (0x08 | table_idx);
  79. }
  80. public virtual void SetConstant (object defaultValue)
  81. {
  82. def_value = defaultValue;
  83. attrs |= ParameterAttributes.HasDefault;
  84. }
  85. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  86. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  87. if (attrname == "System.Runtime.InteropServices.InAttribute") {
  88. attrs |= ParameterAttributes.In;
  89. return;
  90. } else if (attrname == "System.Runtime.InteropServices.OutAttribute") {
  91. attrs |= ParameterAttributes.Out;
  92. return;
  93. } else if (attrname == "System.Runtime.InteropServices.OptionalAttribute") {
  94. attrs |= ParameterAttributes.Optional;
  95. return;
  96. } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
  97. marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
  98. /* FIXME: check for errors */
  99. return;
  100. }
  101. if (cattrs != null) {
  102. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  103. cattrs.CopyTo (new_array, 0);
  104. new_array [cattrs.Length] = customBuilder;
  105. cattrs = new_array;
  106. } else {
  107. cattrs = new CustomAttributeBuilder [1];
  108. cattrs [0] = customBuilder;
  109. }
  110. }
  111. #if NET_2_0
  112. [ComVisible (true)]
  113. #endif
  114. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  115. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  116. }
  117. #if NET_2_0
  118. [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
  119. #endif
  120. public virtual void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
  121. marshal_info = unmanagedMarshal;
  122. attrs |= ParameterAttributes.HasFieldMarshal;
  123. }
  124. }
  125. }