New test.
[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 #if NET_2_0
35         [ComVisible (true)]
36         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
37 #endif
38         [Serializable]
39         [ClassInterfaceAttribute (ClassInterfaceType.None)]
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
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 = "";
62                                 this.PositionImpl = position - 1;
63                                 this.AttrsImpl = ParameterAttributes.None;
64                         }
65                 }
66
67                 /* to build a ParameterInfo for the return type of a method */
68                 internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
69                         this.ClassImpl = type;
70                         this.MemberImpl = member;
71                         this.NameImpl = "";
72                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
73                         this.AttrsImpl = ParameterAttributes.Retval;
74                         this.marshalAs = marshalAs;
75                 }
76
77                 public override string ToString() {
78                         Type elementType = ClassImpl;
79                         while (elementType.HasElementType) {
80                                         elementType = elementType.GetElementType();
81                         }
82                         bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
83                                 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
84                         string result = useShort
85                                 ? ClassImpl.Name
86                                 : ClassImpl.FullName;
87                         // MS.NET seems to skip this check and produce an extra space for return types
88                         if (!IsRetval) {
89                                 result += ' ';
90                                 result += NameImpl;
91                         }
92                         return result;
93                 }
94
95                 public virtual Type ParameterType {
96                         get {return ClassImpl;}
97                 }
98                 public virtual ParameterAttributes Attributes {
99                         get {return AttrsImpl;}
100                 }
101                 public virtual object DefaultValue {
102                         get {return DefaultValueImpl;}
103                 }
104
105                 public bool IsIn {
106                         get {
107 #if NET_2_0
108                                 return (Attributes & ParameterAttributes.In) != 0;
109 #else
110                                 return (AttrsImpl & ParameterAttributes.In) != 0;
111 #endif
112                         }
113                 }
114
115                 public bool IsLcid {
116                         get {
117 #if NET_2_0
118                                 return (Attributes & ParameterAttributes.Lcid) != 0;
119 #else
120                                 return (AttrsImpl & ParameterAttributes.Lcid) != 0;
121 #endif
122                         }
123                 }
124
125                 public bool IsOptional {
126                         get {
127 #if NET_2_0
128                                 return (Attributes & ParameterAttributes.Optional) != 0;
129 #else
130                                 return (AttrsImpl & ParameterAttributes.Optional) != 0;
131 #endif
132                         }
133                 }
134
135                 public bool IsOut {
136                         get {
137 #if NET_2_0
138                                 return (Attributes & ParameterAttributes.Out) != 0;
139 #else
140                                 return (AttrsImpl & ParameterAttributes.Out) != 0;
141 #endif
142                         }
143                 }
144
145                 public bool IsRetval {
146                         get {
147 #if NET_2_0
148                                 return (Attributes & ParameterAttributes.Retval) != 0;
149 #else
150                                 return (AttrsImpl & ParameterAttributes.Retval) != 0;
151 #endif
152                         }
153                 }
154
155                 public virtual MemberInfo Member {
156                         get {return MemberImpl;}
157                 }
158
159                 public virtual string Name {
160                         get {return NameImpl;}
161                 }
162
163                 public virtual int Position {
164                         get {return PositionImpl;}
165                 }
166
167 #if NET_2_0 || BOOTSTRAP_NET_2_0
168                 public
169 #else
170                 internal
171 #endif
172                 virtual 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 #if NET_2_0 || BOOTSTRAP_NET_2_0
222                 [Obsolete ("Use ParameterInfo.GetOptionalCustomModifiers().")]
223                 public virtual Type[] OptionalCustomModifiers {
224                         get {
225                                 return GetOptionalCustomModifiers ();
226                         }
227                 }
228
229                 [Obsolete ("Use ParameterInfo.GetRequiredCustomModifiers().")]
230                 public virtual Type[] RequiredCustomModifiers {
231                         get {
232                                 return GetRequiredCustomModifiers ();
233                         }
234                 }
235
236                 [MonoTODO]
237                 public virtual Type[] GetOptionalCustomModifiers () {
238                         throw new NotImplementedException ();
239                 }
240
241                 [MonoTODO]
242                 public virtual Type[] GetRequiredCustomModifiers () {
243                         throw new NotImplementedException ();
244                 }
245
246                 [MonoTODO]
247                 public virtual object RawDefaultValue {
248                         get {
249                                 throw new NotImplementedException ();
250                         }
251                 }
252 #endif
253
254 #if NET_1_1
255                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
256                 {
257                         throw new NotImplementedException ();
258                 }
259
260                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
261                 {
262                         throw new NotImplementedException ();
263                 }
264
265                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
266                 {
267                         throw new NotImplementedException ();
268                 }
269
270                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
271                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
272                 {
273                         throw new NotImplementedException ();
274                 }
275 #endif
276         }
277 }