Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[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 partial class ParameterInfo : ICustomAttributeProvider {
48 #else
49         public partial 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                 internal MarshalAsAttribute marshalAs;
59
60                 protected ParameterInfo () {
61                 }
62
63                 public override string ToString() {
64                         Type elementType = ClassImpl;
65                         while (elementType.HasElementType) {
66                                         elementType = elementType.GetElementType();
67                         }
68                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
69                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
70                         string result = useShort
71                                 ? ClassImpl.Name
72                                 : ClassImpl.FullName;
73                         // MS.NET seems to skip this check and produce an extra space for return types
74                         if (!IsRetval) {
75                                 result += ' ';
76                                 result += NameImpl;
77                         }
78                         return result;
79                 }
80
81                 internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p)
82                 {
83                         for (int i = 0; i < p.Length; ++i) {
84                                 if (i > 0)
85                                         sb.Append (", ");
86
87                                 Type pt = p[i].ParameterType;
88                                 bool byref = pt.IsByRef;
89                                 if (byref)
90                                         pt = pt.GetElementType ();
91
92                                 if (Type.ShouldPrintFullName (pt))
93                                         sb.Append (pt.ToString ());
94                                 else
95                                         sb.Append (pt.Name);
96
97                                 if (byref)
98                                         sb.Append (" ByRef");
99                         }
100                 }
101
102                 public virtual Type ParameterType {
103                         get {return ClassImpl;}
104                 }
105                 public virtual ParameterAttributes Attributes {
106                         get {return AttrsImpl;}
107                 }
108
109                 public bool IsIn {
110                         get {
111                                 return (Attributes & ParameterAttributes.In) != 0;
112                         }
113                 }
114
115                 public bool IsLcid {
116                         get {
117                                 return (Attributes & ParameterAttributes.Lcid) != 0;
118                         }
119                 }
120
121                 public bool IsOptional {
122                         get {
123                                 return (Attributes & ParameterAttributes.Optional) != 0;
124                         }
125                 }
126
127                 public bool IsOut {
128                         get {
129                                 return (Attributes & ParameterAttributes.Out) != 0;
130                         }
131                 }
132
133                 public bool IsRetval {
134                         get {
135                                 return (Attributes & ParameterAttributes.Retval) != 0;
136                         }
137                 }
138
139                 public virtual MemberInfo Member {
140                         get {return MemberImpl;}
141                 }
142
143                 public virtual string Name {
144                         get {return NameImpl;}
145                 }
146
147                 public virtual int Position {
148                         get {return PositionImpl;}
149                 }
150
151                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152                 internal extern int GetMetadataToken ();
153
154                 internal object[] GetPseudoCustomAttributes () {
155                         int count = 0;
156
157                         if (IsIn)
158                                 count ++;
159                         if (IsOut)
160                                 count ++;
161                         if (IsOptional)
162                                 count ++;
163                         if (marshalAs != null)
164                                 count ++;
165
166                         if (count == 0)
167                                 return null;
168                         object[] attrs = new object [count];
169                         count = 0;
170
171                         if (IsIn)
172                                 attrs [count ++] = new InAttribute ();
173                         if (IsOptional)
174                                 attrs [count ++] = new OptionalAttribute ();
175                         if (IsOut)
176                                 attrs [count ++] = new OutAttribute ();
177
178                         if (marshalAs != null)
179                                 attrs [count ++] = marshalAs.Copy ();
180
181                         return attrs;
182                 }                       
183
184                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185                 internal extern Type[] GetTypeModifiers (bool optional);
186
187                 internal object GetDefaultValueImpl ()
188                 {
189                         return DefaultValueImpl;
190                 }
191
192 #if NET_4_5
193                 public virtual IEnumerable<CustomAttributeData> CustomAttributes {
194                         get { return GetCustomAttributesData (); }
195                 }
196                 
197                 public virtual bool HasDefaultValue {
198                         get { throw new NotImplementedException (); }
199                 }
200 #endif
201
202 #if !MOBILE
203                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
209                 {
210                         throw new NotImplementedException ();
211                 }
212
213                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
214                 {
215                         throw new NotImplementedException ();
216                 }
217
218                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
219                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
220                 {
221                         throw new NotImplementedException ();
222                 }
223 #endif
224
225 #if NET_4_0
226                 public virtual object DefaultValue {
227                         get { throw new NotImplementedException (); }
228                 }
229
230                 public virtual object RawDefaultValue {
231                         get { throw new NotImplementedException (); }
232                 }
233
234                 public virtual int MetadataToken {
235                         get { return 0x8000000; }
236                 }
237
238                 public virtual object[] GetCustomAttributes (bool inherit)
239                 {
240                         return new object [0];
241                 }
242
243                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
244                 {
245                         return new object [0];
246                 }
247
248                 public virtual bool IsDefined( Type attributeType, bool inherit) {
249                         return false;
250                 }
251
252                 public virtual Type[] GetRequiredCustomModifiers () {
253                         return new Type [0];
254                 }
255
256                 public virtual Type[] GetOptionalCustomModifiers () {
257                         return new Type [0];
258                 }
259
260                 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
261                         throw new NotImplementedException ();
262                 }
263 #endif
264
265 #if !FULL_AOT_RUNTIME
266                 internal static ParameterInfo New (ParameterBuilder pb, Type type, MemberInfo member, int position)
267                 {
268 #if NET_4_0
269                         return new MonoParameterInfo (pb, type, member, position);
270 #else
271                         return new ParameterInfo (pb, type, member, position);
272 #endif
273                 }
274 #endif
275
276                 internal static ParameterInfo New (ParameterInfo pinfo, Type type, MemberInfo member, int position)
277                 {
278 #if NET_4_0
279                         return new MonoParameterInfo (pinfo, type, member, position);
280 #else
281                         return new ParameterInfo (pinfo, type, member, position);
282 #endif
283                 }
284
285                 internal static ParameterInfo New (ParameterInfo pinfo, MemberInfo member)
286                 {
287 #if NET_4_0
288                         return new MonoParameterInfo (pinfo, member);
289 #else
290                         return new ParameterInfo (pinfo, member);
291 #endif
292                 }
293
294                 internal static ParameterInfo New (Type type, MemberInfo member, MarshalAsAttribute marshalAs)
295                 {
296 #if NET_4_0
297                         return new MonoParameterInfo (type, member, marshalAs);
298 #else
299                         return new ParameterInfo (type, member, marshalAs);
300 #endif  
301                 }
302         }
303 }