Merge pull request #697 from linquize/atom-bug
[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.DefaultValueImpl = pinfo.DefaultValueImpl;
101                         //this.parent = pinfo;
102                 }
103
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;
108                         this.NameImpl = "";
109                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
110                         this.AttrsImpl = ParameterAttributes.Retval;
111                         this.marshalAs = marshalAs;
112                 }
113
114                 public override string ToString() {
115                         Type elementType = ClassImpl;
116                         while (elementType.HasElementType) {
117                                         elementType = elementType.GetElementType();
118                         }
119                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
120                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
121                         string result = useShort
122                                 ? ClassImpl.Name
123                                 : ClassImpl.FullName;
124                         // MS.NET seems to skip this check and produce an extra space for return types
125                         if (!IsRetval) {
126                                 result += ' ';
127                                 result += NameImpl;
128                         }
129                         return result;
130                 }
131
132                 internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p)
133                 {
134                         for (int i = 0; i < p.Length; ++i) {
135                                 if (i > 0)
136                                         sb.Append (", ");
137
138                                 Type pt = p[i].ParameterType;
139                                 bool byref = pt.IsByRef;
140                                 if (byref)
141                                         pt = pt.GetElementType ();
142
143                                 if (Type.ShouldPrintFullName (pt))
144                                         sb.Append (pt.ToString ());
145                                 else
146                                         sb.Append (pt.Name);
147
148                                 if (byref)
149                                         sb.Append (" ByRef");
150                         }
151                 }
152
153                 public virtual Type ParameterType {
154                         get {return ClassImpl;}
155                 }
156                 public virtual ParameterAttributes Attributes {
157                         get {return AttrsImpl;}
158                 }
159                 public virtual object DefaultValue {
160                         get {
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);
171                                 }
172                                 return DefaultValueImpl;
173                         }
174                 }
175
176                 public bool IsIn {
177                         get {
178                                 return (Attributes & ParameterAttributes.In) != 0;
179                         }
180                 }
181
182                 public bool IsLcid {
183                         get {
184                                 return (Attributes & ParameterAttributes.Lcid) != 0;
185                         }
186                 }
187
188                 public bool IsOptional {
189                         get {
190                                 return (Attributes & ParameterAttributes.Optional) != 0;
191                         }
192                 }
193
194                 public bool IsOut {
195                         get {
196                                 return (Attributes & ParameterAttributes.Out) != 0;
197                         }
198                 }
199
200                 public bool IsRetval {
201                         get {
202                                 return (Attributes & ParameterAttributes.Retval) != 0;
203                         }
204                 }
205
206                 public virtual MemberInfo Member {
207                         get {return MemberImpl;}
208                 }
209
210                 public virtual string Name {
211                         get {return NameImpl;}
212                 }
213
214                 public virtual int Position {
215                         get {return PositionImpl;}
216                 }
217
218                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
219                 extern int GetMetadataToken ();
220
221                 public
222 #if NET_4_0
223                 virtual
224 #endif
225                 int MetadataToken {
226                         get {
227                                 if (MemberImpl is PropertyInfo) {
228                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
229                                         MethodInfo mi = prop.GetGetMethod (true);
230                                         if (mi == null)
231                                                 mi = prop.GetSetMethod (true);
232
233                                         return mi.GetParametersInternal () [PositionImpl].MetadataToken;
234                                 } else if (MemberImpl is MethodBase) {
235                                         return GetMetadataToken ();
236                                 }
237                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
238                         }
239                 }
240
241                 public virtual object[] GetCustomAttributes (bool inherit)
242                 {
243                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
244                 }
245
246                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
247                 {
248                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
249                 }
250
251                 public virtual bool IsDefined( Type attributeType, bool inherit) {
252                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
253                 }
254
255                 internal object[] GetPseudoCustomAttributes () {
256                         int count = 0;
257
258                         if (IsIn)
259                                 count ++;
260                         if (IsOut)
261                                 count ++;
262                         if (IsOptional)
263                                 count ++;
264                         if (marshalAs != null)
265                                 count ++;
266
267                         if (count == 0)
268                                 return null;
269                         object[] attrs = new object [count];
270                         count = 0;
271
272                         if (IsIn)
273                                 attrs [count ++] = new InAttribute ();
274                         if (IsOptional)
275                                 attrs [count ++] = new OptionalAttribute ();
276                         if (IsOut)
277                                 attrs [count ++] = new OutAttribute ();
278
279                         if (marshalAs != null)
280                                 attrs [count ++] = marshalAs.Copy ();
281
282                         return attrs;
283                 }                       
284
285                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286                 extern Type[] GetTypeModifiers (bool optional);
287
288                 public virtual Type[] GetOptionalCustomModifiers () {
289                         Type[] types = GetTypeModifiers (true);
290                         if (types == null)
291                                 return Type.EmptyTypes;
292                         return types;
293                 }
294
295                 public virtual Type[] GetRequiredCustomModifiers () {
296                         Type[] types = GetTypeModifiers (false);
297                         if (types == null)
298                                 return Type.EmptyTypes;
299                         return types;
300                 }
301
302                 public virtual object RawDefaultValue {
303                         get {
304                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
305                                 return DefaultValue;
306                         }
307                 }
308
309 #if NET_4_0
310                 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
311                         return CustomAttributeData.GetCustomAttributes (this);
312                 }
313 #endif
314
315 #if NET_4_5
316                 public virtual IEnumerable<CustomAttributeData> CustomAttributes {
317                         get { return GetCustomAttributesData (); }
318                 }
319
320                 public virtual bool HasDefaultValue {
321                         get { throw new NotImplementedException (); }
322                 }
323 #endif
324
325 #if !MOBILE
326                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
327                 {
328                         throw new NotImplementedException ();
329                 }
330
331                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
332                 {
333                         throw new NotImplementedException ();
334                 }
335
336                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
337                 {
338                         throw new NotImplementedException ();
339                 }
340
341                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
342                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
343                 {
344                         throw new NotImplementedException ();
345                 }
346 #endif
347
348         }
349 }