Merged into single file, added assertions
[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 UnmanagedMarshal marshalAs;
49                 //ParameterInfo parent;
50
51                 protected ParameterInfo () {
52                 }
53
54                 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
55                         this.ClassImpl = type;
56                         this.MemberImpl = member;
57                         if (pb != null) {
58                                 this.NameImpl = pb.Name;
59                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
60                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
61                         } else {
62                                 this.NameImpl = null;
63                                 this.PositionImpl = position - 1;
64                                 this.AttrsImpl = ParameterAttributes.None;
65                         }
66                 }
67
68                 /*FIXME this constructor looks very broken in the position parameter*/
69                 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
70                         this.ClassImpl = type;
71                         this.MemberImpl = member;
72                         if (pinfo != null) {
73                                 this.NameImpl = pinfo.Name;
74                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
75                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
76                         } else {
77                                 this.NameImpl = null;
78                                 this.PositionImpl = position - 1;
79                                 this.AttrsImpl = ParameterAttributes.None;
80                         }
81                 }
82
83                 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
84                         this.ClassImpl = pinfo.ParameterType;
85                         this.MemberImpl = member;
86                         this.NameImpl = pinfo.Name;
87                         this.PositionImpl = pinfo.Position;
88                         this.AttrsImpl = pinfo.Attributes;
89                         //this.parent = pinfo;
90                 }
91
92                 /* to build a ParameterInfo for the return type of a method */
93                 internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
94                         this.ClassImpl = type;
95                         this.MemberImpl = member;
96                         this.NameImpl = "";
97                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
98                         this.AttrsImpl = ParameterAttributes.Retval;
99                         this.marshalAs = marshalAs;
100                 }
101
102                 public override string ToString() {
103                         Type elementType = ClassImpl;
104                         while (elementType.HasElementType) {
105                                         elementType = elementType.GetElementType();
106                         }
107                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
108                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
109                         string result = useShort
110                                 ? ClassImpl.Name
111                                 : ClassImpl.FullName;
112                         // MS.NET seems to skip this check and produce an extra space for return types
113                         if (!IsRetval) {
114                                 result += ' ';
115                                 result += NameImpl;
116                         }
117                         return result;
118                 }
119
120                 public virtual Type ParameterType {
121                         get {return ClassImpl;}
122                 }
123                 public virtual ParameterAttributes Attributes {
124                         get {return AttrsImpl;}
125                 }
126                 public virtual object DefaultValue {
127                         get {
128                                 if (ClassImpl == typeof (Decimal)) {
129                                         /* default values for decimals are encoded using a custom attribute */
130                                         DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
131                                         if (attrs.Length > 0)
132                                                 return attrs [0].Value;
133                                 } else if (ClassImpl == typeof (DateTime)) {
134                                         /* default values for DateTime are encoded using a custom attribute */
135                                         DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
136                                         if (attrs.Length > 0)
137                                                 return new DateTime (attrs [0].Ticks);
138                                 }
139                                 return DefaultValueImpl;
140                         }
141                 }
142
143                 public bool IsIn {
144                         get {
145                                 return (Attributes & ParameterAttributes.In) != 0;
146                         }
147                 }
148
149                 public bool IsLcid {
150                         get {
151                                 return (Attributes & ParameterAttributes.Lcid) != 0;
152                         }
153                 }
154
155                 public bool IsOptional {
156                         get {
157                                 return (Attributes & ParameterAttributes.Optional) != 0;
158                         }
159                 }
160
161                 public bool IsOut {
162                         get {
163                                 return (Attributes & ParameterAttributes.Out) != 0;
164                         }
165                 }
166
167                 public bool IsRetval {
168                         get {
169                                 return (Attributes & ParameterAttributes.Retval) != 0;
170                         }
171                 }
172
173                 public virtual MemberInfo Member {
174                         get {return MemberImpl;}
175                 }
176
177                 public virtual string Name {
178                         get {return NameImpl;}
179                 }
180
181                 public virtual int Position {
182                         get {return PositionImpl;}
183                 }
184
185                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186                 extern int GetMetadataToken ();
187
188                 public
189 #if NET_4_0 || MOONLIGHT
190                 virtual
191 #endif
192                 int MetadataToken {
193                         get {
194                                 if (MemberImpl is PropertyInfo) {
195                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
196                                         MethodInfo mi = prop.GetGetMethod (true);
197                                         if (mi == null)
198                                                 mi = prop.GetSetMethod (true);
199                                         /*TODO expose and use a GetParametersNoCopy()*/
200                                         return mi.GetParameters () [PositionImpl].MetadataToken;
201                                 } else if (MemberImpl is MethodBase) {
202                                         return GetMetadataToken ();
203                                 }
204                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
205                         }
206                 }
207
208                 public virtual object[] GetCustomAttributes (bool inherit)
209                 {
210                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
211                 }
212
213                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
214                 {
215                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
216                 }
217
218                 public virtual bool IsDefined( Type attributeType, bool inherit) {
219                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
220                 }
221
222                 internal object[] GetPseudoCustomAttributes () {
223                         int count = 0;
224
225                         if (IsIn)
226                                 count ++;
227                         if (IsOut)
228                                 count ++;
229                         if (IsOptional)
230                                 count ++;
231                         if (marshalAs != null)
232                                 count ++;
233
234                         if (count == 0)
235                                 return null;
236                         object[] attrs = new object [count];
237                         count = 0;
238
239                         if (IsIn)
240                                 attrs [count ++] = new InAttribute ();
241                         if (IsOptional)
242                                 attrs [count ++] = new OptionalAttribute ();
243                         if (IsOut)
244                                 attrs [count ++] = new OutAttribute ();
245
246                         if (marshalAs != null)
247                                 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
248
249                         return attrs;
250                 }                       
251
252                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
253                 extern Type[] GetTypeModifiers (bool optional);
254
255                 public virtual Type[] GetOptionalCustomModifiers () {
256                         Type[] types = GetTypeModifiers (true);
257                         if (types == null)
258                                 return Type.EmptyTypes;
259                         return types;
260                 }
261
262                 public virtual Type[] GetRequiredCustomModifiers () {
263                         Type[] types = GetTypeModifiers (false);
264                         if (types == null)
265                                 return Type.EmptyTypes;
266                         return types;
267                 }
268
269                 public virtual object RawDefaultValue {
270                         get {
271                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
272                                 return DefaultValue;
273                         }
274                 }
275
276 #if NET_4_0
277                 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
278                         return CustomAttributeData.GetCustomAttributes (this);
279                 }
280 #endif
281
282                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
283                 {
284                         throw new NotImplementedException ();
285                 }
286
287                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
288                 {
289                         throw new NotImplementedException ();
290                 }
291
292                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
293                 {
294                         throw new NotImplementedException ();
295                 }
296
297                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
298                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
299                 {
300                         throw new NotImplementedException ();
301                 }
302         }
303 }