2005-08-30 Sebastien Pouliot <sebastien@ximian.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 #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) {
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                 }
75                 
76                 public virtual Type ParameterType {
77                         get {return ClassImpl;}
78                 }
79                 public virtual ParameterAttributes Attributes {
80                         get {return AttrsImpl;}
81                 }
82                 public virtual object DefaultValue {
83                         get {return DefaultValueImpl;}
84                 }
85
86                 public bool IsIn {
87                         get {return (AttrsImpl & ParameterAttributes.In) != 0;}
88                 }
89
90                 public bool IsLcid {
91                         get {return (AttrsImpl & ParameterAttributes.Lcid) != 0;}
92                 }
93
94                 public bool IsOptional {
95                         get {return (AttrsImpl & ParameterAttributes.Optional) != 0;}
96                 }
97
98                 public bool IsOut {
99                         get {return (AttrsImpl & ParameterAttributes.Out) != 0;}
100                 }
101
102                 public bool IsRetval {
103                         get {return (AttrsImpl & ParameterAttributes.Retval) != 0;}
104                 }
105
106                 public virtual MemberInfo Member {
107                         get {return MemberImpl;}
108                 }
109
110                 public virtual string Name {
111                         get {return NameImpl;}
112                 }
113
114                 public virtual int Position {
115                         get {return PositionImpl;}
116                 }
117
118 #if NET_2_0 || BOOTSTRAP_NET_2_0
119                 public
120 #else
121                 internal
122 #endif
123                 virtual extern int MetadataToken {
124                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
125                         get;
126                 }
127
128                 public virtual object[] GetCustomAttributes (bool inherit)
129                 {
130                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
131                 }
132
133                 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
134                 {
135                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
136                 }
137
138                 public virtual bool IsDefined( Type attributeType, bool inherit) {
139                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
140                 }
141
142                 internal object[] GetPseudoCustomAttributes () {
143                         int count = 0;
144
145                         if (IsIn)
146                                 count ++;
147                         if (IsOut)
148                                 count ++;
149                         if (IsOptional)
150                                 count ++;
151                         if (marshalAs != null)
152                                 count ++;
153
154                         if (count == 0)
155                                 return null;
156                         object[] attrs = new object [count];
157                         count = 0;
158
159                         if (IsIn)
160                                 attrs [count ++] = new InAttribute ();
161                         if (IsOptional)
162                                 attrs [count ++] = new OptionalAttribute ();
163                         if (IsOut)
164                                 attrs [count ++] = new OutAttribute ();
165
166                         if (marshalAs != null)
167                                 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
168
169                         return attrs;
170                 }                       
171
172 #if NET_2_0 || BOOTSTRAP_NET_2_0
173                 [Obsolete ("Use ParameterInfo.GetOptionalCustomModifiers().")]
174                 public virtual Type[] OptionalCustomModifiers {
175                         get {
176                                 return GetOptionalCustomModifiers ();
177                         }
178                 }
179
180                 [Obsolete ("Use ParameterInfo.GetRequiredCustomModifiers().")]
181                 public virtual Type[] RequiredCustomModifiers {
182                         get {
183                                 return GetRequiredCustomModifiers ();
184                         }
185                 }
186
187                 [MonoTODO]
188                 public virtual Type[] GetOptionalCustomModifiers () {
189                         throw new NotImplementedException ();
190                 }
191
192                 [MonoTODO]
193                 public virtual Type[] GetRequiredCustomModifiers () {
194                         throw new NotImplementedException ();
195                 }
196
197                 [MonoTODO]
198                 public virtual object RawDefaultValue {
199                         get {
200                                 throw new NotImplementedException ();
201                         }
202                 }
203 #endif
204
205 #if NET_1_1
206                 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
207                 {
208                         throw new NotImplementedException ();
209                 }
210
211                 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
212                 {
213                         throw new NotImplementedException ();
214                 }
215
216                 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
217                 {
218                         throw new NotImplementedException ();
219                 }
220
221                 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
222                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
223                 {
224                         throw new NotImplementedException ();
225                 }
226 #endif
227         }
228 }