ParameterDefinition.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // ParameterDefinition.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using Mono.Collections.Generic;
  29. namespace Mono.Cecil
  30. {
  31. public sealed class ParameterDefinition : ParameterReference, ICustomAttributeProvider, IConstantProvider, IMarshalInfoProvider
  32. {
  33. ushort attributes;
  34. internal IMethodSignature method;
  35. object constant = Mixin.NotResolved;
  36. Collection<CustomAttribute> custom_attributes;
  37. MarshalInfo marshal_info;
  38. public ParameterAttributes Attributes
  39. {
  40. get { return (ParameterAttributes)attributes; }
  41. set { attributes = (ushort)value; }
  42. }
  43. public IMethodSignature Method
  44. {
  45. get { return method; }
  46. }
  47. public int Sequence
  48. {
  49. get
  50. {
  51. if (method == null)
  52. return -1;
  53. return Mixin.HasImplicitThis(method) ? index + 1 : index;
  54. }
  55. }
  56. public bool HasConstant
  57. {
  58. get
  59. {
  60. Mixin.ResolveConstant(this, ref constant, parameter_type.Module);
  61. return constant != Mixin.NoValue;
  62. }
  63. set { if (!value) constant = Mixin.NoValue; }
  64. }
  65. public object Constant
  66. {
  67. get { return HasConstant ? constant : null; }
  68. set { constant = value; }
  69. }
  70. public bool HasCustomAttributes
  71. {
  72. get
  73. {
  74. if (custom_attributes != null)
  75. return custom_attributes.Count > 0;
  76. return Mixin.GetHasCustomAttributes(this, parameter_type.Module);
  77. }
  78. }
  79. public Collection<CustomAttribute> CustomAttributes
  80. {
  81. get { return custom_attributes ?? (Mixin.GetCustomAttributes(this, ref custom_attributes, parameter_type.Module)); }
  82. }
  83. public bool HasMarshalInfo
  84. {
  85. get
  86. {
  87. if (marshal_info != null)
  88. return true;
  89. return Mixin.GetHasMarshalInfo(this, parameter_type.Module);
  90. }
  91. }
  92. public MarshalInfo MarshalInfo
  93. {
  94. get { return marshal_info ?? (Mixin.GetMarshalInfo(this, ref marshal_info, parameter_type.Module)); }
  95. set { marshal_info = value; }
  96. }
  97. #region ParameterAttributes
  98. public bool IsIn
  99. {
  100. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.In); }
  101. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.In, value); }
  102. }
  103. public bool IsOut
  104. {
  105. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.Out); }
  106. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.Out, value); }
  107. }
  108. public bool IsLcid
  109. {
  110. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.Lcid); }
  111. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.Lcid, value); }
  112. }
  113. public bool IsReturnValue
  114. {
  115. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.Retval); }
  116. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.Retval, value); }
  117. }
  118. public bool IsOptional
  119. {
  120. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.Optional); }
  121. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.Optional, value); }
  122. }
  123. public bool HasDefault
  124. {
  125. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.HasDefault); }
  126. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.HasDefault, value); }
  127. }
  128. public bool HasFieldMarshal
  129. {
  130. get { return Mixin.GetAttributes(attributes, (ushort)ParameterAttributes.HasFieldMarshal); }
  131. set { attributes = Mixin.SetAttributes(attributes, (ushort)ParameterAttributes.HasFieldMarshal, value); }
  132. }
  133. #endregion
  134. internal ParameterDefinition(TypeReference parameterType, IMethodSignature method)
  135. : this(string.Empty, ParameterAttributes.None, parameterType)
  136. {
  137. this.method = method;
  138. }
  139. public ParameterDefinition(TypeReference parameterType)
  140. : this(string.Empty, ParameterAttributes.None, parameterType)
  141. {
  142. }
  143. public ParameterDefinition(string name, ParameterAttributes attributes, TypeReference parameterType)
  144. : base(name, parameterType)
  145. {
  146. this.attributes = (ushort)attributes;
  147. this.token = new MetadataToken(TokenType.Param);
  148. }
  149. public override ParameterDefinition Resolve()
  150. {
  151. return this;
  152. }
  153. }
  154. }