RuntimeParameterInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // System.Reflection.RuntimeParameterInfo
  2. //
  3. // Authors:
  4. // Sean MacIsaac ([email protected])
  5. // Marek Safar ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc.
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. // Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if MONO_FEATURE_SRE
  31. using System.Reflection.Emit;
  32. #endif
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.InteropServices;
  35. using System.Collections.Generic;
  36. using System.Text;
  37. namespace System.Reflection
  38. {
  39. #if !NETCORE
  40. [ComVisible (true)]
  41. [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
  42. [Serializable]
  43. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  44. #endif
  45. class RuntimeParameterInfo : ParameterInfo {
  46. internal MarshalAsAttribute marshalAs;
  47. // Called by the runtime
  48. internal RuntimeParameterInfo (string name, Type type, int position, int attrs, object defaultValue, MemberInfo member, MarshalAsAttribute marshalAs) {
  49. NameImpl = name;
  50. ClassImpl = type;
  51. PositionImpl = position;
  52. AttrsImpl = (ParameterAttributes)attrs;
  53. DefaultValueImpl = defaultValue;
  54. MemberImpl = member;
  55. this.marshalAs = marshalAs;
  56. }
  57. internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p, CallingConventions callingConvention, bool serialization)
  58. {
  59. #if NETCORE
  60. throw new NotImplementedException ();
  61. #else
  62. for (int i = 0; i < p.Length; ++i) {
  63. if (i > 0)
  64. sb.Append (", ");
  65. Type t = p[i].ParameterType;
  66. string typeName = t.FormatTypeName (serialization);
  67. // Legacy: Why use "ByRef" for by ref parameters? What language is this?
  68. // VB uses "ByRef" but it should precede (not follow) the parameter name.
  69. // Why don't we just use "&"?
  70. if (t.IsByRef && !serialization) {
  71. sb.Append (typeName.TrimEnd (new char[] { '&' }));
  72. sb.Append (" ByRef");
  73. } else {
  74. sb.Append (typeName);
  75. }
  76. }
  77. if ((callingConvention & CallingConventions.VarArgs) != 0) {
  78. if (p.Length > 0)
  79. sb.Append (", ");
  80. sb.Append ("...");
  81. }
  82. #endif
  83. }
  84. #if MONO_FEATURE_SRE
  85. internal RuntimeParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
  86. this.ClassImpl = type;
  87. this.MemberImpl = member;
  88. if (pb != null) {
  89. this.NameImpl = pb.Name;
  90. this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
  91. this.AttrsImpl = (ParameterAttributes) pb.Attributes;
  92. } else {
  93. this.NameImpl = null;
  94. this.PositionImpl = position - 1;
  95. this.AttrsImpl = ParameterAttributes.None;
  96. }
  97. }
  98. internal static ParameterInfo New (ParameterBuilder pb, Type type, MemberInfo member, int position)
  99. {
  100. return new RuntimeParameterInfo (pb, type, member, position);
  101. }
  102. #endif
  103. /*FIXME this constructor looks very broken in the position parameter*/
  104. internal RuntimeParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
  105. this.ClassImpl = type;
  106. this.MemberImpl = member;
  107. if (pinfo != null) {
  108. this.NameImpl = pinfo.Name;
  109. this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
  110. this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
  111. } else {
  112. this.NameImpl = null;
  113. this.PositionImpl = position - 1;
  114. this.AttrsImpl = ParameterAttributes.None;
  115. }
  116. }
  117. internal RuntimeParameterInfo (ParameterInfo pinfo, MemberInfo member) {
  118. this.ClassImpl = pinfo.ParameterType;
  119. this.MemberImpl = member;
  120. this.NameImpl = pinfo.Name;
  121. this.PositionImpl = pinfo.Position;
  122. this.AttrsImpl = pinfo.Attributes;
  123. this.DefaultValueImpl = GetDefaultValueImpl (pinfo);
  124. }
  125. /* to build a ParameterInfo for the return type of a method */
  126. internal RuntimeParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
  127. this.ClassImpl = type;
  128. this.MemberImpl = member;
  129. this.NameImpl = null;
  130. this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
  131. this.AttrsImpl = ParameterAttributes.Retval;
  132. this.marshalAs = marshalAs;
  133. }
  134. public override
  135. object DefaultValue {
  136. get {
  137. if (ClassImpl == typeof (Decimal) || ClassImpl == typeof (Decimal?)) {
  138. /* default values for decimals are encoded using a custom attribute */
  139. DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
  140. if (attrs.Length > 0)
  141. return attrs [0].Value;
  142. } else if (ClassImpl == typeof (DateTime) || ClassImpl == typeof (DateTime?)) {
  143. /* default values for DateTime are encoded using a custom attribute */
  144. DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
  145. if (attrs.Length > 0)
  146. return attrs [0].Value;
  147. }
  148. return DefaultValueImpl;
  149. }
  150. }
  151. public override
  152. object RawDefaultValue {
  153. get {
  154. if (DefaultValue != null && DefaultValue.GetType ().IsEnum)
  155. return ((Enum)DefaultValue).GetValue ();
  156. /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
  157. return DefaultValue;
  158. }
  159. }
  160. public
  161. override
  162. int MetadataToken {
  163. get {
  164. if (MemberImpl is PropertyInfo) {
  165. PropertyInfo prop = (PropertyInfo)MemberImpl;
  166. MethodInfo mi = prop.GetGetMethod (true);
  167. if (mi == null)
  168. mi = prop.GetSetMethod (true);
  169. return mi.GetParametersInternal () [PositionImpl].MetadataToken;
  170. } else if (MemberImpl is MethodBase) {
  171. return GetMetadataToken ();
  172. }
  173. throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
  174. }
  175. }
  176. public
  177. override
  178. object[] GetCustomAttributes (bool inherit)
  179. {
  180. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  181. }
  182. public
  183. override
  184. object[] GetCustomAttributes (Type attributeType, bool inherit)
  185. {
  186. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  187. }
  188. internal object GetDefaultValueImpl (ParameterInfo pinfo)
  189. {
  190. FieldInfo field = typeof (ParameterInfo).GetField ("DefaultValueImpl", BindingFlags.Instance | BindingFlags.NonPublic);
  191. return field.GetValue (pinfo);
  192. }
  193. public
  194. override
  195. bool IsDefined( Type attributeType, bool inherit) {
  196. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  197. }
  198. public override IList<CustomAttributeData> GetCustomAttributesData () {
  199. return CustomAttributeData.GetCustomAttributes (this);
  200. }
  201. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  202. internal extern int GetMetadataToken ();
  203. public override Type[] GetOptionalCustomModifiers () => GetCustomModifiers (true);
  204. internal object[] GetPseudoCustomAttributes ()
  205. {
  206. int count = 0;
  207. if (IsIn)
  208. count ++;
  209. if (IsOut)
  210. count ++;
  211. if (IsOptional)
  212. count ++;
  213. if (marshalAs != null)
  214. count ++;
  215. if (count == 0)
  216. return null;
  217. object[] attrs = new object [count];
  218. count = 0;
  219. if (IsIn)
  220. attrs [count ++] = new InAttribute ();
  221. if (IsOut)
  222. attrs [count ++] = new OutAttribute ();
  223. if (IsOptional)
  224. attrs [count ++] = new OptionalAttribute ();
  225. if (marshalAs != null) {
  226. #if NETCORE
  227. throw new NotImplementedException ();
  228. #else
  229. attrs [count ++] = marshalAs.Copy ();
  230. #endif
  231. }
  232. return attrs;
  233. }
  234. internal CustomAttributeData[] GetPseudoCustomAttributesData ()
  235. {
  236. int count = 0;
  237. if (IsIn)
  238. count++;
  239. if (IsOut)
  240. count++;
  241. if (IsOptional)
  242. count++;
  243. if (marshalAs != null)
  244. count++;
  245. if (count == 0)
  246. return null;
  247. CustomAttributeData[] attrsData = new CustomAttributeData [count];
  248. count = 0;
  249. if (IsIn)
  250. attrsData [count++] = new CustomAttributeData ((typeof (InAttribute)).GetConstructor (Type.EmptyTypes));
  251. if (IsOut)
  252. attrsData [count++] = new CustomAttributeData ((typeof (OutAttribute)).GetConstructor (Type.EmptyTypes));
  253. if (IsOptional)
  254. attrsData [count++] = new CustomAttributeData ((typeof (OptionalAttribute)).GetConstructor (Type.EmptyTypes));
  255. if (marshalAs != null) {
  256. var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument (typeof (UnmanagedType), marshalAs.Value) };
  257. attrsData [count++] = new CustomAttributeData (
  258. (typeof (MarshalAsAttribute)).GetConstructor (new[] { typeof (UnmanagedType) }),
  259. ctorArgs,
  260. EmptyArray<CustomAttributeNamedArgument>.Value);//FIXME Get named params
  261. }
  262. return attrsData;
  263. }
  264. public override Type[] GetRequiredCustomModifiers () => GetCustomModifiers (false);
  265. public override bool HasDefaultValue {
  266. get {
  267. object defaultValue = DefaultValue;
  268. if (defaultValue == null)
  269. return true;
  270. if (defaultValue.GetType () == typeof(DBNull) || defaultValue.GetType () == typeof(Missing))
  271. return false;
  272. return true;
  273. }
  274. }
  275. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  276. internal static extern Type[] GetTypeModifiers (Type type, MemberInfo member, int position, bool optional);
  277. internal static ParameterInfo New (ParameterInfo pinfo, Type type, MemberInfo member, int position)
  278. {
  279. return new RuntimeParameterInfo (pinfo, type, member, position);
  280. }
  281. internal static ParameterInfo New (ParameterInfo pinfo, MemberInfo member)
  282. {
  283. return new RuntimeParameterInfo (pinfo, member);
  284. }
  285. internal static ParameterInfo New (Type type, MemberInfo member, MarshalAsAttribute marshalAs)
  286. {
  287. return new RuntimeParameterInfo (type, member, marshalAs);
  288. }
  289. private Type[] GetCustomModifiers (bool optional) => GetTypeModifiers (ParameterType, Member, Position, optional) ?? Type.EmptyTypes;
  290. }
  291. }