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 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
;
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
, UnmanagedMarshal 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 ();
187 public int MetadataToken
{
189 if (MemberImpl
is PropertyInfo
) {
190 PropertyInfo prop
= (PropertyInfo
)MemberImpl
;
191 MethodInfo mi
= prop
.GetGetMethod (true);
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 () {
226 if (marshalAs
!= null)
231 object[] attrs
= new object [count
];
235 attrs
[count
++] = new InAttribute ();
237 attrs
[count
++] = new OptionalAttribute ();
239 attrs
[count
++] = new OutAttribute ();
241 if (marshalAs
!= null)
242 attrs
[count
++] = marshalAs
.ToMarshalAsAttribute ();
247 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
248 extern Type
[] GetTypeModifiers (bool optional
);
250 public virtual Type
[] GetOptionalCustomModifiers () {
251 Type
[] types
= GetTypeModifiers (true);
253 return Type
.EmptyTypes
;
257 public virtual Type
[] GetRequiredCustomModifiers () {
258 Type
[] types
= GetTypeModifiers (false);
260 return Type
.EmptyTypes
;
264 public virtual object RawDefaultValue
{
266 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
272 public virtual IList
<CustomAttributeData
> GetCustomAttributesData () {
273 return CustomAttributeData
.GetCustomAttributes (this);
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 ();