Merge pull request #1504 from madrang/SafeHandle.SetInvalidRelease
[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         [ComVisible (true)]
42         [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
43         [Serializable]
44         [ClassInterfaceAttribute (ClassInterfaceType.None)]
45         [StructLayout (LayoutKind.Sequential)]
46         class MonoParameterInfo : ParameterInfo {
47
48 #if !FULL_AOT_RUNTIME
49                 internal MonoParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
50                         this.ClassImpl = type;
51                         this.MemberImpl = member;
52                         if (pb != null) {
53                                 this.NameImpl = pb.Name;
54                                 this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
55                                 this.AttrsImpl = (ParameterAttributes) pb.Attributes;
56                         } else {
57                                 this.NameImpl = null;
58                                 this.PositionImpl = position - 1;
59                                 this.AttrsImpl = ParameterAttributes.None;
60                         }
61                 }
62 #endif
63
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;
68                         if (pinfo != null) {
69                                 this.NameImpl = pinfo.Name;
70                                 this.PositionImpl = pinfo.Position - 1; // ParameterInfo.Position is zero-based
71                                 this.AttrsImpl = (ParameterAttributes) pinfo.Attributes;
72                         } else {
73                                 this.NameImpl = null;
74                                 this.PositionImpl = position - 1;
75                                 this.AttrsImpl = ParameterAttributes.None;
76                         }
77                 }
78
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;
87                 }
88
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;
93                         this.NameImpl = "";
94                         this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
95                         this.AttrsImpl = ParameterAttributes.Retval;
96                         this.marshalAs = marshalAs;
97                 }
98
99                 public override
100                 object DefaultValue {
101                         get {
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);
112                                 }
113                                 return DefaultValueImpl;
114                         }
115                 }
116
117                 public override
118                 object RawDefaultValue {
119                         get {
120                                 /*FIXME right now DefaultValue doesn't throw for reflection-only assemblies. Change this once the former is fixed.*/
121                                 return DefaultValue;
122                         }
123                 }
124
125                 public
126                 override
127                 int MetadataToken {
128                         get {
129                                 if (MemberImpl is PropertyInfo) {
130                                         PropertyInfo prop = (PropertyInfo)MemberImpl;
131                                         MethodInfo mi = prop.GetGetMethod (true);
132                                         if (mi == null)
133                                                 mi = prop.GetSetMethod (true);
134
135                                         return mi.GetParametersInternal () [PositionImpl].MetadataToken;
136                                 } else if (MemberImpl is MethodBase) {
137                                         return GetMetadataToken ();
138                                 }
139                                 throw new ArgumentException ("Can't produce MetadataToken for member of type " + MemberImpl.GetType ());
140                         }
141                 }
142
143
144                 public
145                 override
146                 object[] GetCustomAttributes (bool inherit)
147                 {
148                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
149                 }
150
151                 public
152                 override
153                 object[] GetCustomAttributes (Type attributeType, bool inherit)
154                 {
155                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
156                 }
157
158
159                 public
160                 override
161                 bool IsDefined( Type attributeType, bool inherit) {
162                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
163                 }
164
165                 public override IList<CustomAttributeData> GetCustomAttributesData () {
166                         return CustomAttributeData.GetCustomAttributes (this);
167                 }
168
169
170                 public
171                 override
172                 Type[] GetOptionalCustomModifiers () {
173                         Type[] types = GetTypeModifiers (true);
174                         if (types == null)
175                                 return Type.EmptyTypes;
176                         return types;
177                 }
178
179                 public
180                 override
181                 Type[] GetRequiredCustomModifiers () {
182                         Type[] types = GetTypeModifiers (false);
183                         if (types == null)
184                                 return Type.EmptyTypes;
185                         return types;
186                 }
187
188                 public override bool HasDefaultValue {
189                         get { 
190                                 object defaultValue = DefaultValue;
191                                 if (defaultValue == null)
192                                         return true;
193
194                                 if (defaultValue.GetType () == typeof(DBNull) || defaultValue.GetType () == typeof(Missing))
195                                         return false;
196
197                                 return true;
198                         }
199                 }
200         }
201 }