ParameterInfo.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. using System.Collections.Generic;
  31. namespace System.Reflection
  32. {
  33. [ComVisible (true)]
  34. [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
  35. [Serializable]
  36. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  37. public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
  38. protected Type ClassImpl;
  39. protected object DefaultValueImpl;
  40. protected MemberInfo MemberImpl;
  41. protected string NameImpl;
  42. protected int PositionImpl;
  43. protected ParameterAttributes AttrsImpl;
  44. private UnmanagedMarshal marshalAs;
  45. //ParameterInfo parent;
  46. protected ParameterInfo () {
  47. }
  48. internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
  49. this.ClassImpl = type;
  50. this.MemberImpl = member;
  51. if (pb != null) {
  52. this.NameImpl = pb.Name;
  53. this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
  54. this.AttrsImpl = (ParameterAttributes) pb.Attributes;
  55. } else {
  56. this.NameImpl = null;
  57. this.PositionImpl = position - 1;
  58. this.AttrsImpl = ParameterAttributes.None;
  59. }
  60. }
  61. /*FIXME this constructor looks very broken in the position parameter*/
  62. internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
  63. this.ClassImpl = type;
  64. this.MemberImpl = member;
  65. if (pinfo != null) {
  66. this.NameImpl = pinfo.Name;
  67. this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
  68. this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
  69. } else {
  70. this.NameImpl = null;
  71. this.PositionImpl = position - 1;
  72. this.AttrsImpl = ParameterAttributes.None;
  73. }
  74. }
  75. internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
  76. this.ClassImpl = pinfo.ParameterType;
  77. this.MemberImpl = member;
  78. this.NameImpl = pinfo.Name;
  79. this.PositionImpl = pinfo.Position;
  80. this.AttrsImpl = pinfo.Attributes;
  81. //this.parent = pinfo;
  82. }
  83. /* to build a ParameterInfo for the return type of a method */
  84. internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
  85. this.ClassImpl = type;
  86. this.MemberImpl = member;
  87. this.NameImpl = "";
  88. this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
  89. this.AttrsImpl = ParameterAttributes.Retval;
  90. this.marshalAs = marshalAs;
  91. }
  92. public override string ToString() {
  93. Type elementType = ClassImpl;
  94. while (elementType.HasElementType) {
  95. elementType = elementType.GetElementType();
  96. }
  97. bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
  98. || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
  99. string result = useShort
  100. ? ClassImpl.Name
  101. : ClassImpl.FullName;
  102. // MS.NET seems to skip this check and produce an extra space for return types
  103. if (!IsRetval) {
  104. result += ' ';
  105. result += NameImpl;
  106. }
  107. return result;
  108. }
  109. public virtual Type ParameterType {
  110. get {return ClassImpl;}
  111. }
  112. public virtual ParameterAttributes Attributes {
  113. get {return AttrsImpl;}
  114. }
  115. public virtual object DefaultValue {
  116. get {
  117. if (ClassImpl == typeof (Decimal)) {
  118. /* default values for decimals are encoded using a custom attribute */
  119. DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
  120. if (attrs.Length > 0)
  121. return attrs [0].Value;
  122. } else if (ClassImpl == typeof (DateTime)) {
  123. /* default values for DateTime are encoded using a custom attribute */
  124. DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
  125. if (attrs.Length > 0)
  126. return new DateTime (attrs [0].Ticks);
  127. }
  128. return DefaultValueImpl;
  129. }
  130. }
  131. public bool IsIn {
  132. get {
  133. return (Attributes & ParameterAttributes.In) != 0;
  134. }
  135. }
  136. public bool IsLcid {
  137. get {
  138. return (Attributes & ParameterAttributes.Lcid) != 0;
  139. }
  140. }
  141. public bool IsOptional {
  142. get {
  143. return (Attributes & ParameterAttributes.Optional) != 0;
  144. }
  145. }
  146. public bool IsOut {
  147. get {
  148. return (Attributes & ParameterAttributes.Out) != 0;
  149. }
  150. }
  151. public bool IsRetval {
  152. get {
  153. return (Attributes & ParameterAttributes.Retval) != 0;
  154. }
  155. }
  156. public virtual MemberInfo Member {
  157. get {return MemberImpl;}
  158. }
  159. public virtual string Name {
  160. get {return NameImpl;}
  161. }
  162. public virtual int Position {
  163. get {return PositionImpl;}
  164. }
  165. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  166. extern int GetMetadataToken ();
  167. public int MetadataToken {
  168. get {
  169. if (MemberImpl is PropertyInfo) {
  170. PropertyInfo prop = (PropertyInfo)MemberImpl;
  171. MethodInfo mi = prop.GetGetMethod (true);
  172. if (mi == null)
  173. mi = prop.GetSetMethod (true);
  174. /*TODO expose and use a GetParametersNoCopy()*/
  175. return mi.GetParameters () [PositionImpl].MetadataToken;
  176. } else if (MemberImpl is MethodBase) {
  177. return GetMetadataToken ();
  178. }
  179. throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
  180. }
  181. }
  182. public virtual object[] GetCustomAttributes (bool inherit)
  183. {
  184. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  185. }
  186. public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
  187. {
  188. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  189. }
  190. public virtual bool IsDefined( Type attributeType, bool inherit) {
  191. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  192. }
  193. internal object[] GetPseudoCustomAttributes () {
  194. int count = 0;
  195. if (IsIn)
  196. count ++;
  197. if (IsOut)
  198. count ++;
  199. if (IsOptional)
  200. count ++;
  201. if (marshalAs != null)
  202. count ++;
  203. if (count == 0)
  204. return null;
  205. object[] attrs = new object [count];
  206. count = 0;
  207. if (IsIn)
  208. attrs [count ++] = new InAttribute ();
  209. if (IsOptional)
  210. attrs [count ++] = new OptionalAttribute ();
  211. if (IsOut)
  212. attrs [count ++] = new OutAttribute ();
  213. if (marshalAs != null)
  214. attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
  215. return attrs;
  216. }
  217. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  218. extern Type[] GetTypeModifiers (bool optional);
  219. public virtual Type[] GetOptionalCustomModifiers () {
  220. Type[] types = GetTypeModifiers (true);
  221. if (types == null)
  222. return Type.EmptyTypes;
  223. return types;
  224. }
  225. public virtual Type[] GetRequiredCustomModifiers () {
  226. Type[] types = GetTypeModifiers (false);
  227. if (types == null)
  228. return Type.EmptyTypes;
  229. return types;
  230. }
  231. public virtual object RawDefaultValue {
  232. get {
  233. /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
  234. return DefaultValue;
  235. }
  236. }
  237. #if NET_4_0
  238. public virtual IList<CustomAttributeData> GetCustomAttributesData () {
  239. return CustomAttributeData.GetCustomAttributes (this);
  240. }
  241. #endif
  242. void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  243. {
  244. throw new NotImplementedException ();
  245. }
  246. void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
  251. {
  252. throw new NotImplementedException ();
  253. }
  254. void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  255. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  256. {
  257. throw new NotImplementedException ();
  258. }
  259. }
  260. }