Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / corlib / System.Reflection / MonoParameterInfo.cs
1 // System.Reflection.ParameterInfo
2 //
3 // Authors:
4 //   Sean MacIsaac (macisaac@ximian.com)
5 //   Marek Safar (marek.safar@gmail.com)
6 //
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)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if !FULL_AOT_RUNTIME
32 using System.Reflection.Emit;
33 #endif
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Collections.Generic;
37 using System.Text;
38
39 namespace System.Reflection
40 {
41 #if NET_4_0
42         [ComVisible (true)]
43         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
44         [Serializable]
45         [ClassInterfaceAttribute (ClassInterfaceType.None)]
46         [StructLayout (LayoutKind.Sequential)]
47         class MonoParameterInfo : ParameterInfo {
48 #else
49         public partial class ParameterInfo {
50 #endif
51
52 #if !FULL_AOT_RUNTIME
53 #if NET_4_0
54                 internal MonoParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
55 #else
56                 internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
57 #endif
58                         this.ClassImpl = type;
59                         this.MemberImpl = member;
60                         if (pb != null) {
61                                 this.NameImpl = pb.Name;
62                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
63                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
64                         } else {
65                                 this.NameImpl = null;
66                                 this.PositionImpl = position - 1;
67                                 this.AttrsImpl = ParameterAttributes.None;
68                         }
69                 }
70 #endif
71
72                 /*FIXME this constructor looks very broken in the position parameter*/
73 #if NET_4_0
74                 internal MonoParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
75 #else
76                 internal ParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
77 #endif
78                         this.ClassImpl = type;
79                         this.MemberImpl = member;
80                         if (pinfo != null) {
81                                 this.NameImpl = pinfo.Name;
82                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
83                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
84                         } else {
85                                 this.NameImpl = null;
86                                 this.PositionImpl = position - 1;
87                                 this.AttrsImpl = ParameterAttributes.None;
88                         }
89                 }
90
91 #if NET_4_0
92                 internal MonoParameterInfo (ParameterInfo pinfo, MemberInfo member) {
93 #else
94                 internal ParameterInfo (ParameterInfo pinfo, MemberInfo member) {
95 #endif
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;
103                 }
104
105                 /* to build a ParameterInfo for the return type of a method */
106 #if NET_4_0
107                 internal MonoParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
108 #else
109                 internal ParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
110 #endif
111                         this.ClassImpl = type;
112                         this.MemberImpl = member;
113                         this.NameImpl = "";
114                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
115                         this.AttrsImpl = ParameterAttributes.Retval;
116                         this.marshalAs = marshalAs;
117                 }
118
119 #if NET_4_0
120                 public override
121 #else
122                 public virtual
123 #endif
124                 object DefaultValue {
125                         get {
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);
136                                 }
137                                 return DefaultValueImpl;
138                         }
139                 }
140
141 #if NET_4_0
142                 public override
143 #else
144                 public
145 #endif
146                 object RawDefaultValue {
147                         get {
148                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
149                                 return DefaultValue;
150                         }
151                 }
152
153                 public
154 #if NET_4_0
155                 override
156 #endif
157                 int MetadataToken {
158                         get {
159                                 if (MemberImpl is PropertyInfo) {
160                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
161                                         MethodInfo mi = prop.GetGetMethod (true);
162                                         if (mi == null)
163                                                 mi = prop.GetSetMethod (true);
164
165                                         return mi.GetParametersInternal () [PositionImpl].MetadataToken;
166                                 } else if (MemberImpl is MethodBase) {
167                                         return GetMetadataToken ();
168                                 }
169                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
170                         }
171                 }
172
173
174                 public
175 #if NET_4_0
176                 override
177 #else
178                 virtual
179 #endif
180                 object[] GetCustomAttributes (bool inherit)
181                 {
182                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
183                 }
184
185                 public
186 #if NET_4_0
187                 override
188 #else
189                 virtual
190 #endif
191                 object[] GetCustomAttributes (Type attributeType, bool inherit)
192                 {
193                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
194                 }
195
196
197                 public
198 #if NET_4_0
199                 override
200 #else
201                 virtual
202 #endif
203                 bool IsDefined( Type attributeType, bool inherit) {
204                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
205                 }
206
207 #if NET_4_0
208                 public override IList<CustomAttributeData> GetCustomAttributesData () {
209                         return CustomAttributeData.GetCustomAttributes (this);
210                 }
211 #endif
212
213
214                 public
215 #if NET_4_0
216                 override
217 #else
218                 virtual
219 #endif
220                 Type[] GetOptionalCustomModifiers () {
221                         Type[] types = GetTypeModifiers (true);
222                         if (types == null)
223                                 return Type.EmptyTypes;
224                         return types;
225                 }
226
227                 public
228 #if NET_4_0
229                 override
230 #else
231                 virtual
232 #endif
233                 Type[] GetRequiredCustomModifiers () {
234                         Type[] types = GetTypeModifiers (false);
235                         if (types == null)
236                                 return Type.EmptyTypes;
237                         return types;
238                 }
239
240 #if NET_4_5
241                 public override bool HasDefaultValue {
242                         get { 
243                                 object defaultValue = DefaultValue;
244                                 if (defaultValue == null)
245                                         return true;
246
247                                 if (defaultValue.GetType () == typeof(DBNull) || defaultValue.GetType () == typeof(Missing))
248                                         return false;
249
250                                 return true;
251                         }
252                 }
253 #endif
254         }
255 }