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.
28 using System.Reflection.Emit;
29 using System.Runtime.CompilerServices;
30 using System.Runtime.InteropServices;
31 using System.Collections.Generic;
33 namespace System.Reflection
36 [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
38 [ClassInterfaceAttribute (ClassInterfaceType.None)]
39 [StructLayout (LayoutKind.Sequential)]
40 public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
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 //ParameterInfo parent;
51 protected ParameterInfo () {
54 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
55 this.ClassImpl = type;
56 this.MemberImpl = member;
58 this.NameImpl = pb.Name;
59 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
60 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
63 this.PositionImpl = position - 1;
64 this.AttrsImpl = ParameterAttributes.None;
68 /*FIXME this constructor looks very broken in the position parameter*/
69 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
70 this.ClassImpl = type;
71 this.MemberImpl = member;
73 this.NameImpl = pinfo.Name;
74 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
75 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
78 this.PositionImpl = position - 1;
79 this.AttrsImpl = ParameterAttributes.None;
83 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
84 this.ClassImpl = pinfo.ParameterType;
85 this.MemberImpl = member;
86 this.NameImpl = pinfo.Name;
87 this.PositionImpl = pinfo.Position;
88 this.AttrsImpl = pinfo.Attributes;
89 //this.parent = pinfo;
92 /* to build a ParameterInfo for the return type of a method */
93 internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
94 this.ClassImpl = type;
95 this.MemberImpl = member;
97 this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
98 this.AttrsImpl = ParameterAttributes.Retval;
99 this.marshalAs = marshalAs;
102 public override string ToString() {
103 Type elementType = ClassImpl;
104 while (elementType.HasElementType) {
105 elementType = elementType.GetElementType();
107 bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
108 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
109 string result = useShort
111 : ClassImpl.FullName;
112 // MS.NET seems to skip this check and produce an extra space for return types
120 public virtual Type ParameterType {
121 get {return ClassImpl;}
123 public virtual ParameterAttributes Attributes {
124 get {return AttrsImpl;}
126 public virtual object DefaultValue {
128 if (ClassImpl == typeof (Decimal)) {
129 /* default values for decimals are encoded using a custom attribute */
130 DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
131 if (attrs.Length > 0)
132 return attrs [0].Value;
133 } else if (ClassImpl == typeof (DateTime)) {
134 /* default values for DateTime are encoded using a custom attribute */
135 DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
136 if (attrs.Length > 0)
137 return new DateTime (attrs [0].Ticks);
139 return DefaultValueImpl;
145 return (Attributes & ParameterAttributes.In) != 0;
151 return (Attributes & ParameterAttributes.Lcid) != 0;
155 public bool IsOptional {
157 return (Attributes & ParameterAttributes.Optional) != 0;
163 return (Attributes & ParameterAttributes.Out) != 0;
167 public bool IsRetval {
169 return (Attributes & ParameterAttributes.Retval) != 0;
173 public virtual MemberInfo Member {
174 get {return MemberImpl;}
177 public virtual string Name {
178 get {return NameImpl;}
181 public virtual int Position {
182 get {return PositionImpl;}
185 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186 extern int GetMetadataToken ();
189 #if NET_4_0 || MOONLIGHT
194 if (MemberImpl is PropertyInfo) {
195 PropertyInfo prop = (PropertyInfo)MemberImpl;
196 MethodInfo mi = prop.GetGetMethod (true);
198 mi = prop.GetSetMethod (true);
199 /*TODO expose and use a GetParametersNoCopy()*/
200 return mi.GetParameters () [PositionImpl].MetadataToken;
201 } else if (MemberImpl is MethodBase) {
202 return GetMetadataToken ();
204 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
208 public virtual object[] GetCustomAttributes (bool inherit)
210 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
213 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
215 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
218 public virtual bool IsDefined( Type attributeType, bool inherit) {
219 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
222 internal object[] GetPseudoCustomAttributes () {
231 if (marshalAs != null)
236 object[] attrs = new object [count];
240 attrs [count ++] = new InAttribute ();
242 attrs [count ++] = new OptionalAttribute ();
244 attrs [count ++] = new OutAttribute ();
246 if (marshalAs != null)
247 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
252 [MethodImplAttribute (MethodImplOptions.InternalCall)]
253 extern Type[] GetTypeModifiers (bool optional);
255 public virtual Type[] GetOptionalCustomModifiers () {
256 Type[] types = GetTypeModifiers (true);
258 return Type.EmptyTypes;
262 public virtual Type[] GetRequiredCustomModifiers () {
263 Type[] types = GetTypeModifiers (false);
265 return Type.EmptyTypes;
269 public virtual object RawDefaultValue {
271 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
277 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
278 return CustomAttributeData.GetCustomAttributes (this);
282 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
284 throw new NotImplementedException ();
287 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
289 throw new NotImplementedException ();
292 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
294 throw new NotImplementedException ();
297 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
298 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
300 throw new NotImplementedException ();