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)]
46 class MonoParameterInfo : ParameterInfo {
49 internal MonoParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
50 this.ClassImpl = type;
51 this.MemberImpl = member;
53 this.NameImpl = pb.Name;
54 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
55 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
58 this.PositionImpl = position - 1;
59 this.AttrsImpl = ParameterAttributes.None;
64 /*FIXME this constructor looks very broken in the position parameter*/
65 internal MonoParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
66 this.ClassImpl = type;
67 this.MemberImpl = member;
69 this.NameImpl = pinfo.Name;
70 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
71 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
74 this.PositionImpl = position - 1;
75 this.AttrsImpl = ParameterAttributes.None;
79 internal MonoParameterInfo (ParameterInfo pinfo, MemberInfo member) {
80 this.ClassImpl = pinfo.ParameterType;
81 this.MemberImpl = member;
82 this.NameImpl = pinfo.Name;
83 this.PositionImpl = pinfo.Position;
84 this.AttrsImpl = pinfo.Attributes;
85 this.DefaultValueImpl = pinfo.GetDefaultValueImpl ();
86 //this.parent = pinfo;
89 /* to build a ParameterInfo for the return type of a method */
90 internal MonoParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
91 this.ClassImpl = type;
92 this.MemberImpl = member;
94 this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
95 this.AttrsImpl = ParameterAttributes.Retval;
96 this.marshalAs = marshalAs;
100 object DefaultValue {
102 if (ClassImpl == typeof (Decimal)) {
103 /* default values for decimals are encoded using a custom attribute */
104 DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
105 if (attrs.Length > 0)
106 return attrs [0].Value;
107 } else if (ClassImpl == typeof (DateTime)) {
108 /* default values for DateTime are encoded using a custom attribute */
109 DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
110 if (attrs.Length > 0)
111 return new DateTime (attrs [0].Ticks);
113 return DefaultValueImpl;
118 object RawDefaultValue {
120 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
129 if (MemberImpl is PropertyInfo) {
130 PropertyInfo prop = (PropertyInfo)MemberImpl;
131 MethodInfo mi = prop.GetGetMethod (true);
133 mi = prop.GetSetMethod (true);
135 return mi.GetParametersInternal () [PositionImpl].MetadataToken;
136 } else if (MemberImpl is MethodBase) {
137 return GetMetadataToken ();
139 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
146 object[] GetCustomAttributes (bool inherit)
148 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
153 object[] GetCustomAttributes (Type attributeType, bool inherit)
155 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
161 bool IsDefined( Type attributeType, bool inherit) {
162 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
165 public override IList<CustomAttributeData> GetCustomAttributesData () {
166 return CustomAttributeData.GetCustomAttributes (this);
172 Type[] GetOptionalCustomModifiers () {
173 Type[] types = GetTypeModifiers (true);
175 return Type.EmptyTypes;
181 Type[] GetRequiredCustomModifiers () {
182 Type[] types = GetTypeModifiers (false);
184 return Type.EmptyTypes;
188 public override bool HasDefaultValue {
190 object defaultValue = DefaultValue;
191 if (defaultValue == null)
194 if (defaultValue.GetType () == typeof(DBNull) || defaultValue.GetType () == typeof(Missing))