ParameterInfo.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // System.Reflection.ParameterInfo
  2. //
  3. // Sean MacIsaac ([email protected])
  4. //
  5. // (C) 2001 Ximian, Inc.
  6. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Reflection.Emit;
  28. using System.Runtime.CompilerServices;
  29. using System.Runtime.InteropServices;
  30. namespace System.Reflection
  31. {
  32. [ComVisible (true)]
  33. [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
  34. [Serializable]
  35. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  36. public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
  37. protected Type ClassImpl;
  38. protected object DefaultValueImpl;
  39. protected MemberInfo MemberImpl;
  40. protected string NameImpl;
  41. protected int PositionImpl;
  42. protected ParameterAttributes AttrsImpl;
  43. private UnmanagedMarshal marshalAs;
  44. protected ParameterInfo () {
  45. }
  46. internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
  47. this.ClassImpl = type;
  48. this.MemberImpl = member;
  49. if (pb != null) {
  50. this.NameImpl = pb.Name;
  51. this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
  52. this.AttrsImpl = (ParameterAttributes) pb.Attributes;
  53. } else {
  54. this.NameImpl = null;
  55. this.PositionImpl = position - 1;
  56. this.AttrsImpl = ParameterAttributes.None;
  57. }
  58. }
  59. internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
  60. this.ClassImpl = type;
  61. this.MemberImpl = member;
  62. if (pinfo != null) {
  63. this.NameImpl = pinfo.Name;
  64. this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
  65. this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
  66. } else {
  67. this.NameImpl = null;
  68. this.PositionImpl = position - 1;
  69. this.AttrsImpl = ParameterAttributes.None;
  70. }
  71. }
  72. /* to build a ParameterInfo for the return type of a method */
  73. internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
  74. this.ClassImpl = type;
  75. this.MemberImpl = member;
  76. this.NameImpl = "";
  77. this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
  78. this.AttrsImpl = ParameterAttributes.Retval;
  79. this.marshalAs = marshalAs;
  80. }
  81. public override string ToString() {
  82. Type elementType = ClassImpl;
  83. while (elementType.HasElementType) {
  84. elementType = elementType.GetElementType();
  85. }
  86. bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
  87. || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
  88. string result = useShort
  89. ? ClassImpl.Name
  90. : ClassImpl.FullName;
  91. // MS.NET seems to skip this check and produce an extra space for return types
  92. if (!IsRetval) {
  93. result += ' ';
  94. result += NameImpl;
  95. }
  96. return result;
  97. }
  98. public virtual Type ParameterType {
  99. get {return ClassImpl;}
  100. }
  101. public virtual ParameterAttributes Attributes {
  102. get {return AttrsImpl;}
  103. }
  104. public virtual object DefaultValue {
  105. get {
  106. if (ClassImpl == typeof (Decimal)) {
  107. /* default values for decimals are encoded using a custom attribute */
  108. DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
  109. if (attrs.Length > 0)
  110. return attrs [0].Value;
  111. }
  112. return DefaultValueImpl;
  113. }
  114. }
  115. public bool IsIn {
  116. get {
  117. return (Attributes & ParameterAttributes.In) != 0;
  118. }
  119. }
  120. public bool IsLcid {
  121. get {
  122. return (Attributes & ParameterAttributes.Lcid) != 0;
  123. }
  124. }
  125. public bool IsOptional {
  126. get {
  127. return (Attributes & ParameterAttributes.Optional) != 0;
  128. }
  129. }
  130. public bool IsOut {
  131. get {
  132. return (Attributes & ParameterAttributes.Out) != 0;
  133. }
  134. }
  135. public bool IsRetval {
  136. get {
  137. return (Attributes & ParameterAttributes.Retval) != 0;
  138. }
  139. }
  140. public virtual MemberInfo Member {
  141. get {return MemberImpl;}
  142. }
  143. public virtual string Name {
  144. get {return NameImpl;}
  145. }
  146. public virtual int Position {
  147. get {return PositionImpl;}
  148. }
  149. public extern int MetadataToken {
  150. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  151. get;
  152. }
  153. public virtual object[] GetCustomAttributes (bool inherit)
  154. {
  155. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  156. }
  157. public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
  158. {
  159. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  160. }
  161. public virtual bool IsDefined( Type attributeType, bool inherit) {
  162. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  163. }
  164. internal object[] GetPseudoCustomAttributes () {
  165. int count = 0;
  166. if (IsIn)
  167. count ++;
  168. if (IsOut)
  169. count ++;
  170. if (IsOptional)
  171. count ++;
  172. if (marshalAs != null)
  173. count ++;
  174. if (count == 0)
  175. return null;
  176. object[] attrs = new object [count];
  177. count = 0;
  178. if (IsIn)
  179. attrs [count ++] = new InAttribute ();
  180. if (IsOptional)
  181. attrs [count ++] = new OptionalAttribute ();
  182. if (IsOut)
  183. attrs [count ++] = new OutAttribute ();
  184. if (marshalAs != null)
  185. attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
  186. return attrs;
  187. }
  188. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  189. extern Type[] GetTypeModifiers (bool optional);
  190. public virtual Type[] GetOptionalCustomModifiers () {
  191. Type[] types = GetTypeModifiers (true);
  192. if (types == null)
  193. return Type.EmptyTypes;
  194. return types;
  195. }
  196. public virtual Type[] GetRequiredCustomModifiers () {
  197. Type[] types = GetTypeModifiers (false);
  198. if (types == null)
  199. return Type.EmptyTypes;
  200. return types;
  201. }
  202. [MonoTODO]
  203. public virtual object RawDefaultValue {
  204. get {
  205. throw new NotImplementedException ();
  206. }
  207. }
  208. void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  209. {
  210. throw new NotImplementedException ();
  211. }
  212. void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  213. {
  214. throw new NotImplementedException ();
  215. }
  216. void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  221. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. }
  226. }