2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Reflection / ParameterInfo.cs
blob6de8aeb01aff97b98b0d619884c40841b77b3e34
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.
28 using System.Reflection.Emit;
29 using System.Runtime.CompilerServices;
30 using System.Runtime.InteropServices;
31 using System.Collections.Generic;
33 namespace System.Reflection
35 [ComVisible (true)]
36 [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
37 [Serializable]
38 [ClassInterfaceAttribute (ClassInterfaceType.None)]
39 public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
41 protected Type ClassImpl;
42 protected object DefaultValueImpl;
43 protected MemberInfo MemberImpl;
44 protected string NameImpl;
45 protected int PositionImpl;
46 protected ParameterAttributes AttrsImpl;
47 private UnmanagedMarshal marshalAs;
48 //ParameterInfo parent;
50 protected ParameterInfo () {
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;
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;
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;
91 /* to build a ParameterInfo for the return type of a method */
92 internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal 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;
101 public override string ToString() {
102 Type elementType = ClassImpl;
103 while (elementType.HasElementType) {
104 elementType = elementType.GetElementType();
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;
116 return result;
119 public virtual Type ParameterType {
120 get {return ClassImpl;}
122 public virtual ParameterAttributes Attributes {
123 get {return AttrsImpl;}
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);
138 return DefaultValueImpl;
142 public bool IsIn {
143 get {
144 return (Attributes & ParameterAttributes.In) != 0;
148 public bool IsLcid {
149 get {
150 return (Attributes & ParameterAttributes.Lcid) != 0;
154 public bool IsOptional {
155 get {
156 return (Attributes & ParameterAttributes.Optional) != 0;
160 public bool IsOut {
161 get {
162 return (Attributes & ParameterAttributes.Out) != 0;
166 public bool IsRetval {
167 get {
168 return (Attributes & ParameterAttributes.Retval) != 0;
172 public virtual MemberInfo Member {
173 get {return MemberImpl;}
176 public virtual string Name {
177 get {return NameImpl;}
180 public virtual int Position {
181 get {return PositionImpl;}
184 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185 extern int GetMetadataToken ();
187 public int MetadataToken {
188 get {
189 if (MemberImpl is PropertyInfo) {
190 PropertyInfo prop = (PropertyInfo)MemberImpl;
191 MethodInfo mi = prop.GetGetMethod (true);
192 if (mi == null)
193 mi = prop.GetSetMethod (true);
194 /*TODO expose and use a GetParametersNoCopy()*/
195 return mi.GetParameters () [PositionImpl].MetadataToken;
196 } else if (MemberImpl is MethodBase) {
197 return GetMetadataToken ();
199 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
203 public virtual object[] GetCustomAttributes (bool inherit)
205 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
208 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
210 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
213 public virtual bool IsDefined( Type attributeType, bool inherit) {
214 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
217 internal object[] GetPseudoCustomAttributes () {
218 int count = 0;
220 if (IsIn)
221 count ++;
222 if (IsOut)
223 count ++;
224 if (IsOptional)
225 count ++;
226 if (marshalAs != null)
227 count ++;
229 if (count == 0)
230 return null;
231 object[] attrs = new object [count];
232 count = 0;
234 if (IsIn)
235 attrs [count ++] = new InAttribute ();
236 if (IsOptional)
237 attrs [count ++] = new OptionalAttribute ();
238 if (IsOut)
239 attrs [count ++] = new OutAttribute ();
241 if (marshalAs != null)
242 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
244 return attrs;
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 extern Type[] GetTypeModifiers (bool optional);
250 public virtual Type[] GetOptionalCustomModifiers () {
251 Type[] types = GetTypeModifiers (true);
252 if (types == null)
253 return Type.EmptyTypes;
254 return types;
257 public virtual Type[] GetRequiredCustomModifiers () {
258 Type[] types = GetTypeModifiers (false);
259 if (types == null)
260 return Type.EmptyTypes;
261 return types;
264 public virtual object RawDefaultValue {
265 get {
266 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
267 return DefaultValue;
271 #if NET_4_0
272 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
273 return CustomAttributeData.GetCustomAttributes (this);
275 #endif
277 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
279 throw new NotImplementedException ();
282 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
284 throw new NotImplementedException ();
287 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
289 throw new NotImplementedException ();
292 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
293 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
295 throw new NotImplementedException ();