Merge pull request #1631 from alexanderkyte/stringbuilder-referencesource
[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         abstract class RuntimeParameterInfo : ParameterInfo
42         {
43
44         }
45
46         [ComVisible (true)]
47         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
48         [Serializable]
49         [ClassInterfaceAttribute (ClassInterfaceType.None)]
50         [StructLayout (LayoutKind.Sequential)]
51         class MonoParameterInfo : RuntimeParameterInfo {
52
53 #if !FULL_AOT_RUNTIME
54                 internal MonoParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
55                         this.ClassImpl = type;
56                         this.MemberImpl = member;
57                         if (pb != null) {
58                                 this.NameImpl = pb.Name;
59                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
60                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
61                         } else {
62                                 this.NameImpl = null;
63                                 this.PositionImpl = position - 1;
64                                 this.AttrsImpl = ParameterAttributes.None;
65                         }
66                 }
67 #endif
68
69                 /*FIXME this constructor looks very broken in the position parameter*/
70                 internal MonoParameterInfo (ParameterInfo pinfo, Type type, MemberInfo member, int position) {
71                         this.ClassImpl = type;
72                         this.MemberImpl = member;
73                         if (pinfo != null) {
74                                 this.NameImpl = pinfo.Name;
75                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
76                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
77                         } else {
78                                 this.NameImpl = null;
79                                 this.PositionImpl = position - 1;
80                                 this.AttrsImpl = ParameterAttributes.None;
81                         }
82                 }
83
84                 internal MonoParameterInfo (ParameterInfo pinfo, MemberInfo member) {
85                         this.ClassImpl = pinfo.ParameterType;
86                         this.MemberImpl = member;
87                         this.NameImpl = pinfo.Name;
88                         this.PositionImpl = pinfo.Position;
89                         this.AttrsImpl = pinfo.Attributes;
90                         this.DefaultValueImpl = pinfo.GetDefaultValueImpl ();
91                         //this.parent = pinfo;
92                 }
93
94                 /* to build a ParameterInfo for the return type of a method */
95                 internal MonoParameterInfo (Type type, MemberInfo member, MarshalAsAttribute marshalAs) {
96                         this.ClassImpl = type;
97                         this.MemberImpl = member;
98                         this.NameImpl = "";
99                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
100                         this.AttrsImpl = ParameterAttributes.Retval;
101                         this.marshalAs = marshalAs;
102                 }
103
104                 public override
105                 object DefaultValue {
106                         get {
107                                 if (ClassImpl == typeof (Decimal)) {
108                                         /* default values for decimals are encoded using a custom attribute */
109                                         DecimalConstantAttribute[] attrs = (DecimalConstantAttribute[])GetCustomAttributes (typeof (DecimalConstantAttribute), false);
110                                         if (attrs.Length > 0)
111                                                 return attrs [0].Value;
112                                 } else if (ClassImpl == typeof (DateTime)) {
113                                         /* default values for DateTime are encoded using a custom attribute */
114                                         DateTimeConstantAttribute[] attrs = (DateTimeConstantAttribute[])GetCustomAttributes (typeof (DateTimeConstantAttribute), false);
115                                         if (attrs.Length > 0)
116                                                 return attrs [0].Value;
117                                 }
118                                 return DefaultValueImpl;
119                         }
120                 }
121
122                 public override
123                 object RawDefaultValue {
124                         get {
125                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
126                                 return DefaultValue;
127                         }
128                 }
129
130                 public
131                 override
132                 int MetadataToken {
133                         get {
134                                 if (MemberImpl is PropertyInfo) {
135                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
136                                         MethodInfo mi = prop.GetGetMethod (true);
137                                         if (mi == null)
138                                                 mi = prop.GetSetMethod (true);
139
140                                         return mi.GetParametersInternal () [PositionImpl].MetadataToken;
141                                 } else if (MemberImpl is MethodBase) {
142                                         return GetMetadataToken ();
143                                 }
144                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
145                         }
146                 }
147
148
149                 public
150                 override
151                 object[] GetCustomAttributes (bool inherit)
152                 {
153                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
154                 }
155
156                 public
157                 override
158                 object[] GetCustomAttributes (Type attributeType, bool inherit)
159                 {
160                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
161                 }
162
163
164                 public
165                 override
166                 bool IsDefined( Type attributeType, bool inherit) {
167                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
168                 }
169
170                 public override IList<CustomAttributeData> GetCustomAttributesData () {
171                         return CustomAttributeData.GetCustomAttributes (this);
172                 }
173
174
175                 public
176                 override
177                 Type[] GetOptionalCustomModifiers () {
178                         Type[] types = GetTypeModifiers (true);
179                         if (types == null)
180                                 return Type.EmptyTypes;
181                         return types;
182                 }
183
184                 public
185                 override
186                 Type[] GetRequiredCustomModifiers () {
187                         Type[] types = GetTypeModifiers (false);
188                         if (types == null)
189                                 return Type.EmptyTypes;
190                         return types;
191                 }
192
193                 public override bool HasDefaultValue {
194                         get { 
195                                 object defaultValue = DefaultValue;
196                                 if (defaultValue == null)
197                                         return true;
198
199                                 if (defaultValue.GetType () == typeof(DBNull) || defaultValue.GetType () == typeof(Missing))
200                                         return false;
201
202                                 return true;
203                         }
204                 }
205         }
206 }