2010-01-05 Rodrigo Kumpera <rkumpera@novell.com>
[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
32 namespace System.Reflection
33 {
34         [ComVisible (true)]
35         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
36         [Serializable]
37         [ClassInterfaceAttribute (ClassInterfaceType.None)]
38         public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
39
40                 protected Type ClassImpl;
41                 protected object DefaultValueImpl;
42                 protected MemberInfo MemberImpl;
43                 protected string NameImpl;
44                 protected int PositionImpl;
45                 protected ParameterAttributes AttrsImpl;
46                 private UnmanagedMarshal marshalAs;
47
48                 protected ParameterInfo () {
49                 }
50
51                 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
52                         this.ClassImpl = type;
53                         this.MemberImpl = member;
54                         if (pb != null) {
55                                 this.NameImpl = pb.Name;
56                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
57                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
58                         } else {
59                                 this.NameImpl = null;
60                                 this.PositionImpl = position - 1;
61                                 this.AttrsImpl = ParameterAttributes.None;
62                         }
63                 }
64
65                 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
66                         this.ClassImpl = type;
67                         this.MemberImpl = member;
68                         if (pinfo != null) {
69                                 this.NameImpl = pinfo.Name;
70                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
71                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
72                         } else {
73                                 this.NameImpl = null;
74                                 this.PositionImpl = position - 1;
75                                 this.AttrsImpl = ParameterAttributes.None;
76                         }
77                 }
78
79                 /* to build a ParameterInfo for the return type of a method */
80                 internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
81                         this.ClassImpl = type;
82                         this.MemberImpl = member;
83                         this.NameImpl = "";
84                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
85                         this.AttrsImpl = ParameterAttributes.Retval;
86                         this.marshalAs = marshalAs;
87                 }
88
89                 public override string ToString() {
90                         Type elementType = ClassImpl;
91                         while (elementType.HasElementType) {
92                                         elementType = elementType.GetElementType();
93                         }
94                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
95                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
96                         string result = useShort
97                                 ? ClassImpl.Name
98                                 : ClassImpl.FullName;
99                         // MS.NET seems to skip this check and produce an extra space for return types
100                         if (!IsRetval) {
101                                 result += ' ';
102                                 result += NameImpl;
103                         }
104                         return result;
105                 }
106
107                 public virtual Type ParameterType {
108                         get {return ClassImpl;}
109                 }
110                 public virtual ParameterAttributes Attributes {
111                         get {return AttrsImpl;}
112                 }
113                 public virtual object DefaultValue {
114                         get {
115                                 if (ClassImpl == typeof (Decimal)) {
116                                         /* default values for decimals are encoded using a custom attribute */
117                                         DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
118                                         if (attrs.Length > 0)
119                                                 return attrs [0].Value;
120                                 } else if (ClassImpl == typeof (DateTime)) {
121                                         /* default values for DateTime are encoded using a custom attribute */
122                                         DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
123                                         if (attrs.Length > 0)
124                                                 return new DateTime (attrs [0].Ticks);
125                                 }
126                                 return DefaultValueImpl;
127                         }
128                 }
129
130                 public bool IsIn {
131                         get {
132                                 return (Attributes & ParameterAttributes.In) != 0;
133                         }
134                 }
135
136                 public bool IsLcid {
137                         get {
138                                 return (Attributes & ParameterAttributes.Lcid) != 0;
139                         }
140                 }
141
142                 public bool IsOptional {
143                         get {
144                                 return (Attributes & ParameterAttributes.Optional) != 0;
145                         }
146                 }
147
148                 public bool IsOut {
149                         get {
150                                 return (Attributes & ParameterAttributes.Out) != 0;
151                         }
152                 }
153
154                 public bool IsRetval {
155                         get {
156                                 return (Attributes & ParameterAttributes.Retval) != 0;
157                         }
158                 }
159
160                 public virtual MemberInfo Member {
161                         get {return MemberImpl;}
162                 }
163
164                 public virtual string Name {
165                         get {return NameImpl;}
166                 }
167
168                 public virtual int Position {
169                         get {return PositionImpl;}
170                 }
171
172                 public extern int MetadataToken {
173                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
174                         get;
175                 }
176
177                 public virtual object[] GetCustomAttributes (bool inherit)
178                 {
179                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
180                 }
181
182                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
183                 {
184                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
185                 }
186
187                 public virtual bool IsDefined( Type attributeType, bool inherit) {
188                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
189                 }
190
191                 internal object[] GetPseudoCustomAttributes () {
192                         int count = 0;
193
194                         if (IsIn)
195                                 count ++;
196                         if (IsOut)
197                                 count ++;
198                         if (IsOptional)
199                                 count ++;
200                         if (marshalAs != null)
201                                 count ++;
202
203                         if (count == 0)
204                                 return null;
205                         object[] attrs = new object [count];
206                         count = 0;
207
208                         if (IsIn)
209                                 attrs [count ++] = new InAttribute ();
210                         if (IsOptional)
211                                 attrs [count ++] = new OptionalAttribute ();
212                         if (IsOut)
213                                 attrs [count ++] = new OutAttribute ();
214
215                         if (marshalAs != null)
216                                 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
217
218                         return attrs;
219                 }                       
220
221                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
222                 extern Type[] GetTypeModifiers (bool optional);
223
224                 public virtual Type[] GetOptionalCustomModifiers () {
225                         Type[] types = GetTypeModifiers (true);
226                         if (types == null)
227                                 return Type.EmptyTypes;
228                         return types;
229                 }
230
231                 public virtual Type[] GetRequiredCustomModifiers () {
232                         Type[] types = GetTypeModifiers (false);
233                         if (types == null)
234                                 return Type.EmptyTypes;
235                         return types;
236                 }
237
238                 public virtual object RawDefaultValue {
239                         get {
240                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
241                                 return DefaultValue;
242                         }
243                 }
244
245                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
246                 {
247                         throw new NotImplementedException ();
248                 }
249
250                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
251                 {
252                         throw new NotImplementedException ();
253                 }
254
255                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
256                 {
257                         throw new NotImplementedException ();
258                 }
259
260                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
261                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
262                 {
263                         throw new NotImplementedException ();
264                 }
265         }
266 }