1 // System.Reflection.ParameterInfo
4 // Sean MacIsaac (macisaac@ximian.com)
5 // Marek Safar (marek.safar@gmail.com)
7 // (C) 2001 Ximian, Inc.
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection.Emit;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Collections.Generic;
39 namespace System.Reflection
42 [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
44 [ClassInterfaceAttribute (ClassInterfaceType.None)]
45 [StructLayout (LayoutKind.Sequential)]
47 public class ParameterInfo : ICustomAttributeProvider {
49 public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
52 protected Type ClassImpl;
53 protected object DefaultValueImpl;
54 protected MemberInfo MemberImpl;
55 protected string NameImpl;
56 protected int PositionImpl;
57 protected ParameterAttributes AttrsImpl;
58 private MarshalAsAttribute marshalAs;
60 protected ParameterInfo () {
64 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
65 this.ClassImpl = type;
66 this.MemberImpl = member;
68 this.NameImpl = pb.Name;
69 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
70 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
73 this.PositionImpl = position - 1;
74 this.AttrsImpl = ParameterAttributes.None;
79 /*FIXME this constructor looks very broken in the position parameter*/
80 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
81 this.ClassImpl = type;
82 this.MemberImpl = member;
84 this.NameImpl = pinfo.Name;
85 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
86 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
89 this.PositionImpl = position - 1;
90 this.AttrsImpl = ParameterAttributes.None;
94 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
95 this.ClassImpl = pinfo.ParameterType;
96 this.MemberImpl = member;
97 this.NameImpl = pinfo.Name;
98 this.PositionImpl = pinfo.Position;
99 this.AttrsImpl = pinfo.Attributes;
100 //this.parent = pinfo;
103 /* to build a ParameterInfo for the return type of a method */
104 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
105 this.ClassImpl = type;
106 this.MemberImpl = member;
108 this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
109 this.AttrsImpl = ParameterAttributes.Retval;
110 this.marshalAs = marshalAs;
113 public override string ToString() {
114 Type elementType = ClassImpl;
115 while (elementType.HasElementType) {
116 elementType = elementType.GetElementType();
118 bool useShort = elementType.IsPrimitive || ClassImpl == typeof(void)
119 || ClassImpl.Namespace == MemberImpl.DeclaringType.Namespace;
120 string result = useShort
122 : ClassImpl.FullName;
123 // MS.NET seems to skip this check and produce an extra space for return types
131 internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p)
133 for (int i = 0; i < p.Length; ++i) {
137 Type pt = p[i].ParameterType;
138 bool byref = pt.IsByRef;
140 pt = pt.GetElementType ();
142 if (Type.ShouldPrintFullName (pt))
143 sb.Append (pt.ToString ());
148 sb.Append (" ByRef");
152 public virtual Type ParameterType {
153 get {return ClassImpl;}
155 public virtual ParameterAttributes Attributes {
156 get {return AttrsImpl;}
158 public virtual object DefaultValue {
160 if (ClassImpl == typeof (Decimal)) {
161 /* default values for decimals are encoded using a custom attribute */
162 DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
163 if (attrs.Length > 0)
164 return attrs [0].Value;
165 } else if (ClassImpl == typeof (DateTime)) {
166 /* default values for DateTime are encoded using a custom attribute */
167 DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
168 if (attrs.Length > 0)
169 return new DateTime (attrs [0].Ticks);
171 return DefaultValueImpl;
177 return (Attributes & ParameterAttributes.In) != 0;
183 return (Attributes & ParameterAttributes.Lcid) != 0;
187 public bool IsOptional {
189 return (Attributes & ParameterAttributes.Optional) != 0;
195 return (Attributes & ParameterAttributes.Out) != 0;
199 public bool IsRetval {
201 return (Attributes & ParameterAttributes.Retval) != 0;
205 public virtual MemberInfo Member {
206 get {return MemberImpl;}
209 public virtual string Name {
210 get {return NameImpl;}
213 public virtual int Position {
214 get {return PositionImpl;}
217 [MethodImplAttribute (MethodImplOptions.InternalCall)]
218 extern int GetMetadataToken ();
226 if (MemberImpl is PropertyInfo) {
227 PropertyInfo prop = (PropertyInfo)MemberImpl;
228 MethodInfo mi = prop.GetGetMethod (true);
230 mi = prop.GetSetMethod (true);
232 return mi.GetParametersInternal () [PositionImpl].MetadataToken;
233 } else if (MemberImpl is MethodBase) {
234 return GetMetadataToken ();
236 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
240 public virtual object[] GetCustomAttributes (bool inherit)
242 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
245 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
247 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
250 public virtual bool IsDefined( Type attributeType, bool inherit) {
251 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
254 internal object[] GetPseudoCustomAttributes () {
263 if (marshalAs != null)
268 object[] attrs = new object [count];
272 attrs [count ++] = new InAttribute ();
274 attrs [count ++] = new OptionalAttribute ();
276 attrs [count ++] = new OutAttribute ();
278 if (marshalAs != null)
279 attrs [count ++] = marshalAs.Copy ();
284 [MethodImplAttribute (MethodImplOptions.InternalCall)]
285 extern Type[] GetTypeModifiers (bool optional);
287 public virtual Type[] GetOptionalCustomModifiers () {
288 Type[] types = GetTypeModifiers (true);
290 return Type.EmptyTypes;
294 public virtual Type[] GetRequiredCustomModifiers () {
295 Type[] types = GetTypeModifiers (false);
297 return Type.EmptyTypes;
301 public virtual object RawDefaultValue {
303 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
309 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
310 return CustomAttributeData.GetCustomAttributes (this);
315 public virtual IEnumerable<CustomAttributeData> CustomAttributes {
316 get { return GetCustomAttributesData (); }
319 public virtual bool HasDefaultValue {
320 get { throw new NotImplementedException (); }
325 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
327 throw new NotImplementedException ();
330 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
332 throw new NotImplementedException ();
335 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
337 throw new NotImplementedException ();
340 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
341 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
343 throw new NotImplementedException ();