1 // System.Reflection.ParameterInfo
3 // Sean MacIsaac (macisaac@ximian.com)
5 // (C) 2001 Ximian, Inc.
6 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
29 using System.Reflection.Emit;
31 using System.Runtime.CompilerServices;
32 using System.Runtime.InteropServices;
33 using System.Collections.Generic;
35 namespace System.Reflection
38 [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
40 [ClassInterfaceAttribute (ClassInterfaceType.None)]
41 [StructLayout (LayoutKind.Sequential)]
42 public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
44 protected Type ClassImpl;
45 protected object DefaultValueImpl;
46 protected MemberInfo MemberImpl;
47 protected string NameImpl;
48 protected int PositionImpl;
49 protected ParameterAttributes AttrsImpl;
50 private MarshalAsAttribute marshalAs;
52 protected ParameterInfo () {
56 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
57 this.ClassImpl = type;
58 this.MemberImpl = member;
60 this.NameImpl = pb.Name;
61 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
62 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
65 this.PositionImpl = position - 1;
66 this.AttrsImpl = ParameterAttributes.None;
71 /*FIXME this constructor looks very broken in the position parameter*/
72 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
73 this.ClassImpl = type;
74 this.MemberImpl = member;
76 this.NameImpl = pinfo.Name;
77 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
78 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
81 this.PositionImpl = position - 1;
82 this.AttrsImpl = ParameterAttributes.None;
86 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
87 this.ClassImpl = pinfo.ParameterType;
88 this.MemberImpl = member;
89 this.NameImpl = pinfo.Name;
90 this.PositionImpl = pinfo.Position;
91 this.AttrsImpl = pinfo.Attributes;
92 //this.parent = pinfo;
95 /* to build a ParameterInfo for the return type of a method */
96 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
97 this.ClassImpl = type;
98 this.MemberImpl = member;
100 this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
101 this.AttrsImpl = ParameterAttributes.Retval;
102 this.marshalAs = marshalAs;
105 public override string ToString() {
106 Type elementType = ClassImpl;
107 while (elementType.HasElementType) {
108 elementType = elementType.GetElementType();
110 bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
111 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
112 string result = useShort
114 : ClassImpl.FullName;
115 // MS.NET seems to skip this check and produce an extra space for return types
123 public virtual Type ParameterType {
124 get {return ClassImpl;}
126 public virtual ParameterAttributes Attributes {
127 get {return AttrsImpl;}
129 public virtual object DefaultValue {
131 if (ClassImpl == typeof (Decimal)) {
132 /* default values for decimals are encoded using a custom attribute */
133 DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
134 if (attrs.Length > 0)
135 return attrs [0].Value;
136 } else if (ClassImpl == typeof (DateTime)) {
137 /* default values for DateTime are encoded using a custom attribute */
138 DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
139 if (attrs.Length > 0)
140 return new DateTime (attrs [0].Ticks);
142 return DefaultValueImpl;
148 return (Attributes & ParameterAttributes.In) != 0;
154 return (Attributes & ParameterAttributes.Lcid) != 0;
158 public bool IsOptional {
160 return (Attributes & ParameterAttributes.Optional) != 0;
166 return (Attributes & ParameterAttributes.Out) != 0;
170 public bool IsRetval {
172 return (Attributes & ParameterAttributes.Retval) != 0;
176 public virtual MemberInfo Member {
177 get {return MemberImpl;}
180 public virtual string Name {
181 get {return NameImpl;}
184 public virtual int Position {
185 get {return PositionImpl;}
188 [MethodImplAttribute (MethodImplOptions.InternalCall)]
189 extern int GetMetadataToken ();
192 #if NET_4_0 || MOONLIGHT
197 if (MemberImpl is PropertyInfo) {
198 PropertyInfo prop = (PropertyInfo)MemberImpl;
199 MethodInfo mi = prop.GetGetMethod (true);
201 mi = prop.GetSetMethod (true);
202 /*TODO expose and use a GetParametersNoCopy()*/
203 return mi.GetParameters () [PositionImpl].MetadataToken;
204 } else if (MemberImpl is MethodBase) {
205 return GetMetadataToken ();
207 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
211 public virtual object[] GetCustomAttributes (bool inherit)
213 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
216 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
218 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
221 public virtual bool IsDefined( Type attributeType, bool inherit) {
222 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
225 internal object[] GetPseudoCustomAttributes () {
234 if (marshalAs != null)
239 object[] attrs = new object [count];
243 attrs [count ++] = new InAttribute ();
245 attrs [count ++] = new OptionalAttribute ();
247 attrs [count ++] = new OutAttribute ();
249 if (marshalAs != null)
250 attrs [count ++] = marshalAs.Copy ();
255 [MethodImplAttribute (MethodImplOptions.InternalCall)]
256 extern Type[] GetTypeModifiers (bool optional);
258 public virtual Type[] GetOptionalCustomModifiers () {
259 Type[] types = GetTypeModifiers (true);
261 return Type.EmptyTypes;
265 public virtual Type[] GetRequiredCustomModifiers () {
266 Type[] types = GetTypeModifiers (false);
268 return Type.EmptyTypes;
272 public virtual object RawDefaultValue {
274 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
280 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
281 return CustomAttributeData.GetCustomAttributes (this);
285 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
287 throw new NotImplementedException ();
290 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
292 throw new NotImplementedException ();
295 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
297 throw new NotImplementedException ();
300 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
301 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
303 throw new NotImplementedException ();