Merge pull request #600 from tr8dr/master
[mono.git] / mcs / class / corlib / System.Reflection / ParameterInfo.cs
1 // System.Reflection.ParameterInfo
2 //
3 // Authors:
4 //   Sean MacIsaac (macisaac@ximian.com)
5 //   Marek Safar (marek.safar@gmail.com)
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
31 #if !FULL_AOT_RUNTIME
32 using System.Reflection.Emit;
33 #endif
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Collections.Generic;
37 using System.Text;
38
39 namespace System.Reflection
40 {
41         [ComVisible (true)]
42         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
43         [Serializable]
44         [ClassInterfaceAttribute (ClassInterfaceType.None)]
45         [StructLayout (LayoutKind.Sequential)]
46 #if MOBILE
47         public class ParameterInfo : ICustomAttributeProvider {
48 #else
49         public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
50 #endif
51
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;
59
60                 protected ParameterInfo () {
61                 }
62
63 #if !FULL_AOT_RUNTIME
64                 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
65                         this.ClassImpl = type;
66                         this.MemberImpl = member;
67                         if (pb != null) {
68                                 this.NameImpl = pb.Name;
69                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
70                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
71                         } else {
72                                 this.NameImpl = null;
73                                 this.PositionImpl = position - 1;
74                                 this.AttrsImpl = ParameterAttributes.None;
75                         }
76                 }
77 #endif
78
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;
83                         if (pinfo != null) {
84                                 this.NameImpl = pinfo.Name;
85                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
86                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
87                         } else {
88                                 this.NameImpl = null;
89                                 this.PositionImpl = position - 1;
90                                 this.AttrsImpl = ParameterAttributes.None;
91                         }
92                 }
93
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.parent = pinfo;
101                 }
102
103                 /* to build a ParameterInfo for the return type of a method */
104                 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
105                         this.ClassImpl = type;
106                         this.MemberImpl = member;
107                         this.NameImpl = "";
108                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
109                         this.AttrsImpl = ParameterAttributes.Retval;
110                         this.marshalAs = marshalAs;
111                 }
112
113                 public override string ToString() {
114                         Type elementType = ClassImpl;
115                         while (elementType.HasElementType) {
116                                         elementType = elementType.GetElementType();
117                         }
118                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
119                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
120                         string result = useShort
121                                 ? ClassImpl.Name
122                                 : ClassImpl.FullName;
123                         // MS.NET seems to skip this check and produce an extra space for return types
124                         if (!IsRetval) {
125                                 result += ' ';
126                                 result += NameImpl;
127                         }
128                         return result;
129                 }
130
131                 internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p)
132                 {
133                         for (int i = 0; i < p.Length; ++i) {
134                                 if (i > 0)
135                                         sb.Append (", ");
136
137                                 Type pt = p[i].ParameterType;
138                                 bool byref = pt.IsByRef;
139                                 if (byref)
140                                         pt = pt.GetElementType ();
141
142                                 if (Type.ShouldPrintFullName (pt))
143                                         sb.Append (pt.ToString ());
144                                 else
145                                         sb.Append (pt.Name);
146
147                                 if (byref)
148                                         sb.Append (" ByRef");
149                         }
150                 }
151
152                 public virtual Type ParameterType {
153                         get {return ClassImpl;}
154                 }
155                 public virtual ParameterAttributes Attributes {
156                         get {return AttrsImpl;}
157                 }
158                 public virtual object DefaultValue {
159                         get {
160                                 if (ClassImpl == typeof (Decimal)) {
161                                         /* default values for decimals are encoded using a custom attribute */
162                                         DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
163                                         if (attrs.Length > 0)
164                                                 return attrs [0].Value;
165                                 } else if (ClassImpl == typeof (DateTime)) {
166                                         /* default values for DateTime are encoded using a custom attribute */
167                                         DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
168                                         if (attrs.Length > 0)
169                                                 return new DateTime (attrs [0].Ticks);
170                                 }
171                                 return DefaultValueImpl;
172                         }
173                 }
174
175                 public bool IsIn {
176                         get {
177                                 return (Attributes & ParameterAttributes.In) != 0;
178                         }
179                 }
180
181                 public bool IsLcid {
182                         get {
183                                 return (Attributes & ParameterAttributes.Lcid) != 0;
184                         }
185                 }
186
187                 public bool IsOptional {
188                         get {
189                                 return (Attributes & ParameterAttributes.Optional) != 0;
190                         }
191                 }
192
193                 public bool IsOut {
194                         get {
195                                 return (Attributes & ParameterAttributes.Out) != 0;
196                         }
197                 }
198
199                 public bool IsRetval {
200                         get {
201                                 return (Attributes & ParameterAttributes.Retval) != 0;
202                         }
203                 }
204
205                 public virtual MemberInfo Member {
206                         get {return MemberImpl;}
207                 }
208
209                 public virtual string Name {
210                         get {return NameImpl;}
211                 }
212
213                 public virtual int Position {
214                         get {return PositionImpl;}
215                 }
216
217                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
218                 extern int GetMetadataToken ();
219
220                 public
221 #if NET_4_0
222                 virtual
223 #endif
224                 int MetadataToken {
225                         get {
226                                 if (MemberImpl is PropertyInfo) {
227                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
228                                         MethodInfo mi = prop.GetGetMethod (true);
229                                         if (mi == null)
230                                                 mi = prop.GetSetMethod (true);
231
232                                         return mi.GetParametersInternal () [PositionImpl].MetadataToken;
233                                 } else if (MemberImpl is MethodBase) {
234                                         return GetMetadataToken ();
235                                 }
236                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
237                         }
238                 }
239
240                 public virtual object[] GetCustomAttributes (bool inherit)
241                 {
242                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
243                 }
244
245                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
246                 {
247                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
248                 }
249
250                 public virtual bool IsDefined( Type attributeType, bool inherit) {
251                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
252                 }
253
254                 internal object[] GetPseudoCustomAttributes () {
255                         int count = 0;
256
257                         if (IsIn)
258                                 count ++;
259                         if (IsOut)
260                                 count ++;
261                         if (IsOptional)
262                                 count ++;
263                         if (marshalAs != null)
264                                 count ++;
265
266                         if (count == 0)
267                                 return null;
268                         object[] attrs = new object [count];
269                         count = 0;
270
271                         if (IsIn)
272                                 attrs [count ++] = new InAttribute ();
273                         if (IsOptional)
274                                 attrs [count ++] = new OptionalAttribute ();
275                         if (IsOut)
276                                 attrs [count ++] = new OutAttribute ();
277
278                         if (marshalAs != null)
279                                 attrs [count ++] = marshalAs.Copy ();
280
281                         return attrs;
282                 }                       
283
284                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
285                 extern Type[] GetTypeModifiers (bool optional);
286
287                 public virtual Type[] GetOptionalCustomModifiers () {
288                         Type[] types = GetTypeModifiers (true);
289                         if (types == null)
290                                 return Type.EmptyTypes;
291                         return types;
292                 }
293
294                 public virtual Type[] GetRequiredCustomModifiers () {
295                         Type[] types = GetTypeModifiers (false);
296                         if (types == null)
297                                 return Type.EmptyTypes;
298                         return types;
299                 }
300
301                 public virtual object RawDefaultValue {
302                         get {
303                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
304                                 return DefaultValue;
305                         }
306                 }
307
308 #if NET_4_0
309                 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
310                         return CustomAttributeData.GetCustomAttributes (this);
311                 }
312 #endif
313
314 #if NET_4_5
315                 public virtual IEnumerable<CustomAttributeData> CustomAttributes {
316                         get { return GetCustomAttributesData (); }
317                 }
318
319                 public virtual bool HasDefaultValue {
320                         get { throw new NotImplementedException (); }
321                 }
322 #endif
323
324 #if !MOBILE
325                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
326                 {
327                         throw new NotImplementedException ();
328                 }
329
330                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
331                 {
332                         throw new NotImplementedException ();
333                 }
334
335                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
336                 {
337                         throw new NotImplementedException ();
338                 }
339
340                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
341                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
342                 {
343                         throw new NotImplementedException ();
344                 }
345 #endif
346
347         }
348 }