1 // System.Reflection.ParameterInfo
4 // Sean MacIsaac (macisaac@ximian.com)
5 // Marek Safar (marek.safar@gmail.com)
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)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Reflection.Emit;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Collections.Generic;
39 namespace System.Reflection
42 [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
44 [ClassInterfaceAttribute (ClassInterfaceType.None)]
45 [StructLayout (LayoutKind.Sequential)]
47 public class ParameterInfo : ICustomAttributeProvider {
49 public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
52 protected Type ClassImpl;
53 protected object DefaultValueImpl;
54 protected MemberInfo MemberImpl;
55 protected string NameImpl;
56 protected int PositionImpl;
57 protected ParameterAttributes AttrsImpl;
58 private MarshalAsAttribute marshalAs;
60 protected ParameterInfo () {
64 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
65 this.ClassImpl = type;
66 this.MemberImpl = member;
68 this.NameImpl = pb.Name;
69 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
70 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
73 this.PositionImpl = position - 1;
74 this.AttrsImpl = ParameterAttributes.None;
79 /*FIXME this constructor looks very broken in the position parameter*/
80 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
81 this.ClassImpl = type;
82 this.MemberImpl = member;
84 this.NameImpl = pinfo.Name;
85 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
86 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
89 this.PositionImpl = position - 1;
90 this.AttrsImpl = ParameterAttributes.None;
94 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
95 this.ClassImpl = pinfo.ParameterType;
96 this.MemberImpl = member;
97 this.NameImpl = pinfo.Name;
98 this.PositionImpl = pinfo.Position;
99 this.AttrsImpl = pinfo.Attributes;
100 this.DefaultValueImpl = pinfo.DefaultValueImpl;
101 //this.parent = pinfo;
104 /* to build a ParameterInfo for the return type of a method */
105 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
106 this.ClassImpl = type;
107 this.MemberImpl = member;
109 this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
110 this.AttrsImpl = ParameterAttributes.Retval;
111 this.marshalAs = marshalAs;
114 public override string ToString() {
115 Type elementType = ClassImpl;
116 while (elementType.HasElementType) {
117 elementType = elementType.GetElementType();
119 bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
120 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
121 string result = useShort
123 : ClassImpl.FullName;
124 // MS.NET seems to skip this check and produce an extra space for return types
132 internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p)
134 for (int i = 0; i < p.Length; ++i) {
138 Type pt = p[i].ParameterType;
139 bool byref = pt.IsByRef;
141 pt = pt.GetElementType ();
143 if (Type.ShouldPrintFullName (pt))
144 sb.Append (pt.ToString ());
149 sb.Append (" ByRef");
153 public virtual Type ParameterType {
154 get {return ClassImpl;}
156 public virtual ParameterAttributes Attributes {
157 get {return AttrsImpl;}
159 public virtual object DefaultValue {
161 if (ClassImpl == typeof (Decimal)) {
162 /* default values for decimals are encoded using a custom attribute */
163 DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
164 if (attrs.Length > 0)
165 return attrs [0].Value;
166 } else if (ClassImpl == typeof (DateTime)) {
167 /* default values for DateTime are encoded using a custom attribute */
168 DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
169 if (attrs.Length > 0)
170 return new DateTime (attrs [0].Ticks);
172 return DefaultValueImpl;
178 return (Attributes & ParameterAttributes.In) != 0;
184 return (Attributes & ParameterAttributes.Lcid) != 0;
188 public bool IsOptional {
190 return (Attributes & ParameterAttributes.Optional) != 0;
196 return (Attributes & ParameterAttributes.Out) != 0;
200 public bool IsRetval {
202 return (Attributes & ParameterAttributes.Retval) != 0;
206 public virtual MemberInfo Member {
207 get {return MemberImpl;}
210 public virtual string Name {
211 get {return NameImpl;}
214 public virtual int Position {
215 get {return PositionImpl;}
218 [MethodImplAttribute (MethodImplOptions.InternalCall)]
219 extern int GetMetadataToken ();
227 if (MemberImpl is PropertyInfo) {
228 PropertyInfo prop = (PropertyInfo)MemberImpl;
229 MethodInfo mi = prop.GetGetMethod (true);
231 mi = prop.GetSetMethod (true);
233 return mi.GetParametersInternal () [PositionImpl].MetadataToken;
234 } else if (MemberImpl is MethodBase) {
235 return GetMetadataToken ();
237 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
241 public virtual object[] GetCustomAttributes (bool inherit)
243 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
246 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
248 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
251 public virtual bool IsDefined( Type attributeType, bool inherit) {
252 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
255 internal object[] GetPseudoCustomAttributes () {
264 if (marshalAs != null)
269 object[] attrs = new object [count];
273 attrs [count ++] = new InAttribute ();
275 attrs [count ++] = new OptionalAttribute ();
277 attrs [count ++] = new OutAttribute ();
279 if (marshalAs != null)
280 attrs [count ++] = marshalAs.Copy ();
285 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286 extern Type[] GetTypeModifiers (bool optional);
288 public virtual Type[] GetOptionalCustomModifiers () {
289 Type[] types = GetTypeModifiers (true);
291 return Type.EmptyTypes;
295 public virtual Type[] GetRequiredCustomModifiers () {
296 Type[] types = GetTypeModifiers (false);
298 return Type.EmptyTypes;
302 public virtual object RawDefaultValue {
304 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
310 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
311 return CustomAttributeData.GetCustomAttributes (this);
316 public virtual IEnumerable<CustomAttributeData> CustomAttributes {
317 get { return GetCustomAttributesData (); }
320 public virtual bool HasDefaultValue {
321 get { throw new NotImplementedException (); }
326 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
328 throw new NotImplementedException ();
331 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
333 throw new NotImplementedException ();
336 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
338 throw new NotImplementedException ();
341 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
342 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
344 throw new NotImplementedException ();