Merge remote-tracking branch 'upstream/master' into fix-for-issue2907-with-no-formatt...
[mono.git] / mcs / class / corlib / System.Reflection / ParameterInfo.cs
1 // System.Reflection.ParameterInfo
2 //
3 // Sean MacIsaac (macisaac@ximian.com)
4 //
5 // (C) 2001 Ximian, Inc.
6 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28 using System.Reflection.Emit;
29 using System.Runtime.CompilerServices;
30 using System.Runtime.InteropServices;
31 using System.Collections.Generic;
32
33 namespace System.Reflection
34 {
35         [ComVisible (true)]
36         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
37         [Serializable]
38         [ClassInterfaceAttribute (ClassInterfaceType.None)]
39         [StructLayout (LayoutKind.Sequential)]
40         public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
41
42                 protected Type ClassImpl;
43                 protected object DefaultValueImpl;
44                 protected MemberInfo MemberImpl;
45                 protected string NameImpl;
46                 protected int PositionImpl;
47                 protected ParameterAttributes AttrsImpl;
48                 private MarshalAsAttribute marshalAs;
49
50                 protected ParameterInfo () {
51                 }
52
53                 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
54                         this.ClassImpl = type;
55                         this.MemberImpl = member;
56                         if (pb != null) {
57                                 this.NameImpl = pb.Name;
58                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
59                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
60                         } else {
61                                 this.NameImpl = null;
62                                 this.PositionImpl = position - 1;
63                                 this.AttrsImpl = ParameterAttributes.None;
64                         }
65                 }
66
67                 /*FIXME this constructor looks very broken in the position parameter*/
68                 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
69                         this.ClassImpl = type;
70                         this.MemberImpl = member;
71                         if (pinfo != null) {
72                                 this.NameImpl = pinfo.Name;
73                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
74                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
75                         } else {
76                                 this.NameImpl = null;
77                                 this.PositionImpl = position - 1;
78                                 this.AttrsImpl = ParameterAttributes.None;
79                         }
80                 }
81
82                 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
83                         this.ClassImpl = pinfo.ParameterType;
84                         this.MemberImpl = member;
85                         this.NameImpl = pinfo.Name;
86                         this.PositionImpl = pinfo.Position;
87                         this.AttrsImpl = pinfo.Attributes;
88                         //this.parent = pinfo;
89                 }
90
91                 /* to build a ParameterInfo for the return type of a method */
92                 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
93                         this.ClassImpl = type;
94                         this.MemberImpl = member;
95                         this.NameImpl = "";
96                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
97                         this.AttrsImpl = ParameterAttributes.Retval;
98                         this.marshalAs = marshalAs;
99                 }
100
101                 public override string ToString() {
102                         Type elementType = ClassImpl;
103                         while (elementType.HasElementType) {
104                                         elementType = elementType.GetElementType();
105                         }
106                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
107                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
108                         string result = useShort
109                                 ? ClassImpl.Name
110                                 : ClassImpl.FullName;
111                         // MS.NET seems to skip this check and produce an extra space for return types
112                         if (!IsRetval) {
113                                 result += ' ';
114                                 result += NameImpl;
115                         }
116                         return result;
117                 }
118
119                 public virtual Type ParameterType {
120                         get {return ClassImpl;}
121                 }
122                 public virtual ParameterAttributes Attributes {
123                         get {return AttrsImpl;}
124                 }
125                 public virtual object DefaultValue {
126                         get {
127                                 if (ClassImpl == typeof (Decimal)) {
128                                         /* default values for decimals are encoded using a custom attribute */
129                                         DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
130                                         if (attrs.Length > 0)
131                                                 return attrs [0].Value;
132                                 } else if (ClassImpl == typeof (DateTime)) {
133                                         /* default values for DateTime are encoded using a custom attribute */
134                                         DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
135                                         if (attrs.Length > 0)
136                                                 return new DateTime (attrs [0].Ticks);
137                                 }
138                                 return DefaultValueImpl;
139                         }
140                 }
141
142                 public bool IsIn {
143                         get {
144                                 return (Attributes & ParameterAttributes.In) != 0;
145                         }
146                 }
147
148                 public bool IsLcid {
149                         get {
150                                 return (Attributes & ParameterAttributes.Lcid) != 0;
151                         }
152                 }
153
154                 public bool IsOptional {
155                         get {
156                                 return (Attributes & ParameterAttributes.Optional) != 0;
157                         }
158                 }
159
160                 public bool IsOut {
161                         get {
162                                 return (Attributes & ParameterAttributes.Out) != 0;
163                         }
164                 }
165
166                 public bool IsRetval {
167                         get {
168                                 return (Attributes & ParameterAttributes.Retval) != 0;
169                         }
170                 }
171
172                 public virtual MemberInfo Member {
173                         get {return MemberImpl;}
174                 }
175
176                 public virtual string Name {
177                         get {return NameImpl;}
178                 }
179
180                 public virtual int Position {
181                         get {return PositionImpl;}
182                 }
183
184                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185                 extern int GetMetadataToken ();
186
187                 public
188 #if NET_4_0 || MOONLIGHT
189                 virtual
190 #endif
191                 int MetadataToken {
192                         get {
193                                 if (MemberImpl is PropertyInfo) {
194                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
195                                         MethodInfo mi = prop.GetGetMethod (true);
196                                         if (mi == null)
197                                                 mi = prop.GetSetMethod (true);
198                                         /*TODO expose and use a GetParametersNoCopy()*/
199                                         return mi.GetParameters () [PositionImpl].MetadataToken;
200                                 } else if (MemberImpl is MethodBase) {
201                                         return GetMetadataToken ();
202                                 }
203                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
204                         }
205                 }
206
207                 public virtual object[] GetCustomAttributes (bool inherit)
208                 {
209                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
210                 }
211
212                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
213                 {
214                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
215                 }
216
217                 public virtual bool IsDefined( Type attributeType, bool inherit) {
218                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
219                 }
220
221                 internal object[] GetPseudoCustomAttributes () {
222                         int count = 0;
223
224                         if (IsIn)
225                                 count ++;
226                         if (IsOut)
227                                 count ++;
228                         if (IsOptional)
229                                 count ++;
230                         if (marshalAs != null)
231                                 count ++;
232
233                         if (count == 0)
234                                 return null;
235                         object[] attrs = new object [count];
236                         count = 0;
237
238                         if (IsIn)
239                                 attrs [count ++] = new InAttribute ();
240                         if (IsOptional)
241                                 attrs [count ++] = new OptionalAttribute ();
242                         if (IsOut)
243                                 attrs [count ++] = new OutAttribute ();
244
245                         if (marshalAs != null)
246                                 attrs [count ++] = marshalAs.Copy ();
247
248                         return attrs;
249                 }                       
250
251                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
252                 extern Type[] GetTypeModifiers (bool optional);
253
254                 public virtual Type[] GetOptionalCustomModifiers () {
255                         Type[] types = GetTypeModifiers (true);
256                         if (types == null)
257                                 return Type.EmptyTypes;
258                         return types;
259                 }
260
261                 public virtual Type[] GetRequiredCustomModifiers () {
262                         Type[] types = GetTypeModifiers (false);
263                         if (types == null)
264                                 return Type.EmptyTypes;
265                         return types;
266                 }
267
268                 public virtual object RawDefaultValue {
269                         get {
270                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
271                                 return DefaultValue;
272                         }
273                 }
274
275 #if NET_4_0
276                 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
277                         return CustomAttributeData.GetCustomAttributes (this);
278                 }
279 #endif
280
281                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
282                 {
283                         throw new NotImplementedException ();
284                 }
285
286                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
287                 {
288                         throw new NotImplementedException ();
289                 }
290
291                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
292                 {
293                         throw new NotImplementedException ();
294                 }
295
296                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
297                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
298                 {
299                         throw new NotImplementedException ();
300                 }
301         }
302 }