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
43 [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
45 [ClassInterfaceAttribute (ClassInterfaceType.None)]
46 [StructLayout (LayoutKind.Sequential)]
47 class MonoParameterInfo : ParameterInfo {
49 public partial class ParameterInfo {
54 internal MonoParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
56 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
58 this.ClassImpl = type;
59 this.MemberImpl = member;
61 this.NameImpl = pb.Name;
62 this.PositionImpl = pb.Position - 1; // ParameterInfo.Position is zero-based
63 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
66 this.PositionImpl = position - 1;
67 this.AttrsImpl = ParameterAttributes.None;
72 /*FIXME this constructor looks very broken in the position parameter*/
74 internal MonoParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
76 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
78 this.ClassImpl = type;
79 this.MemberImpl = member;
81 this.NameImpl = pinfo.Name;
82 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
83 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
86 this.PositionImpl = position - 1;
87 this.AttrsImpl = ParameterAttributes.None;
92 internal MonoParameterInfo (ParameterInfo pinfo, MemberInfo member) {
94 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
96 this.ClassImpl = pinfo.ParameterType;
97 this.MemberImpl = member;
98 this.NameImpl = pinfo.Name;
99 this.PositionImpl = pinfo.Position;
100 this.AttrsImpl = pinfo.Attributes;
101 this.DefaultValueImpl = pinfo.GetDefaultValueImpl ();
102 //this.parent = pinfo;
105 /* to build a ParameterInfo for the return type of a method */
107 internal MonoParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
109 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
111 this.ClassImpl = type;
112 this.MemberImpl = member;
114 this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
115 this.AttrsImpl = ParameterAttributes.Retval;
116 this.marshalAs = marshalAs;
124 object DefaultValue {
126 if (ClassImpl == typeof (Decimal)) {
127 /* default values for decimals are encoded using a custom attribute */
128 DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
129 if (attrs.Length > 0)
130 return attrs [0].Value;
131 } else if (ClassImpl == typeof (DateTime)) {
132 /* default values for DateTime are encoded using a custom attribute */
133 DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
134 if (attrs.Length > 0)
135 return new DateTime (attrs [0].Ticks);
137 return DefaultValueImpl;
146 object RawDefaultValue {
148 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
159 if (MemberImpl is PropertyInfo) {
160 PropertyInfo prop = (PropertyInfo)MemberImpl;
161 MethodInfo mi = prop.GetGetMethod (true);
163 mi = prop.GetSetMethod (true);
165 return mi.GetParametersInternal () [PositionImpl].MetadataToken;
166 } else if (MemberImpl is MethodBase) {
167 return GetMetadataToken ();
169 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
180 object[] GetCustomAttributes (bool inherit)
182 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
191 object[] GetCustomAttributes (Type attributeType, bool inherit)
193 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
203 bool IsDefined( Type attributeType, bool inherit) {
204 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
208 public override IList<CustomAttributeData> GetCustomAttributesData () {
209 return CustomAttributeData.GetCustomAttributes (this);
220 Type[] GetOptionalCustomModifiers () {
221 Type[] types = GetTypeModifiers (true);
223 return Type.EmptyTypes;
233 Type[] GetRequiredCustomModifiers () {
234 Type[] types = GetTypeModifiers (false);
236 return Type.EmptyTypes;