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 MarshalAsAttribute marshalAs;
50 protected ParameterInfo () {
53 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
54 this.ClassImpl = type;
55 this.MemberImpl = member;
57 this.NameImpl = pb.Name;
58 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
59 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
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;
72 this.NameImpl = pinfo.Name;
73 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
74 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
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, MarshalAsAttribute marshalAs) {
93 this.ClassImpl = type;
94 this.MemberImpl = member;
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
110 : ClassImpl.FullName;
111 // MS.NET seems to skip this check and produce an extra space for return types
119 public virtual Type ParameterType {
120 get {return ClassImpl;}
122 public virtual ParameterAttributes Attributes {
123 get {return AttrsImpl;}
125 public virtual object DefaultValue {
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;
144 return (Attributes & ParameterAttributes.In) != 0;
150 return (Attributes & ParameterAttributes.Lcid) != 0;
154 public bool IsOptional {
156 return (Attributes & ParameterAttributes.Optional) != 0;
162 return (Attributes & ParameterAttributes.Out) != 0;
166 public bool IsRetval {
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 ();
188 #if NET_4_0 || MOONLIGHT
193 if (MemberImpl is PropertyInfo) {
194 PropertyInfo prop = (PropertyInfo)MemberImpl;
195 MethodInfo mi = prop.GetGetMethod (true);
197 mi = prop.GetSetMethod (true);
198 /*TODO expose and use a GetParametersNoCopy()*/
199 return mi.GetParameters () [PositionImpl].MetadataToken;
200 } else if (MemberImpl is MethodBase) {
201 return GetMetadataToken ();
203 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
207 public virtual object[] GetCustomAttributes (bool inherit)
209 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
212 public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
214 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
217 public virtual bool IsDefined( Type attributeType, bool inherit) {
218 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
221 internal object[] GetPseudoCustomAttributes () {
230 if (marshalAs != null)
235 object[] attrs = new object [count];
239 attrs [count ++] = new InAttribute ();
241 attrs [count ++] = new OptionalAttribute ();
243 attrs [count ++] = new OutAttribute ();
245 if (marshalAs != null)
246 attrs [count ++] = marshalAs.Copy ();
251 [MethodImplAttribute (MethodImplOptions.InternalCall)]
252 extern Type[] GetTypeModifiers (bool optional);
254 public virtual Type[] GetOptionalCustomModifiers () {
255 Type[] types = GetTypeModifiers (true);
257 return Type.EmptyTypes;
261 public virtual Type[] GetRequiredCustomModifiers () {
262 Type[] types = GetTypeModifiers (false);
264 return Type.EmptyTypes;
268 public virtual object RawDefaultValue {
270 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
276 public virtual IList<CustomAttributeData> GetCustomAttributesData () {
277 return CustomAttributeData.GetCustomAttributes (this);
281 void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
283 throw new NotImplementedException ();
286 void _ParameterInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
288 throw new NotImplementedException ();
291 void _ParameterInfo.GetTypeInfoCount (out uint pcTInfo)
293 throw new NotImplementedException ();
296 void _ParameterInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
297 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
299 throw new NotImplementedException ();