[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / corlib / System.Reflection / FieldInfo.cs
1 //
2 // System.Reflection.FieldInfo.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2011 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 using System.Diagnostics;
31 using System.Globalization;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
34
35 namespace System.Reflection {
36
37         [ComVisible (true)]
38         [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
39         [Serializable]
40         [ClassInterface(ClassInterfaceType.None)]
41 #if MOBILE
42         public abstract class FieldInfo : MemberInfo {
43 #else
44         public abstract class FieldInfo : MemberInfo, _FieldInfo {
45 #endif
46                 public abstract FieldAttributes Attributes {get;}
47                 public abstract RuntimeFieldHandle FieldHandle {get;}
48
49                 protected FieldInfo () {}
50                 
51                 public abstract Type FieldType { get; }
52
53                 public abstract object GetValue(object obj);
54
55                 public override MemberTypes MemberType {
56                         get { return MemberTypes.Field;}
57                 }
58
59                 public bool IsLiteral
60                 {
61                         get {return (Attributes & FieldAttributes.Literal) != 0;}
62                 } 
63
64                 public bool IsStatic
65                 {
66                         get {return (Attributes & FieldAttributes.Static) != 0;}
67                 } 
68
69                 public bool IsInitOnly
70                 {
71                         get {return (Attributes & FieldAttributes.InitOnly) != 0;}
72                 } 
73                 public Boolean IsPublic
74                 { 
75                         get
76                         {
77                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
78                         }
79                 }
80                 public Boolean IsPrivate
81                 {
82                         get
83                         {
84                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
85                         }
86                 }
87                 public Boolean IsFamily
88                 {
89                         get
90                         {
91                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
92                         }
93                 }
94                 public Boolean IsAssembly
95                 {
96                         get
97                         {
98                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
99                         }
100                 }
101                 public Boolean IsFamilyAndAssembly
102                 {
103                         get {
104                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
105                         }
106                 }
107                 public Boolean IsFamilyOrAssembly
108                 {
109                         get
110                         {
111                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
112                         }
113                 }
114                 public Boolean IsPinvokeImpl
115                 {
116                         get
117                         {
118                                 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
119                         }
120                 }
121                 public Boolean IsSpecialName
122                 {
123                         get
124                         {
125                                 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
126                         }
127                 }
128                 public Boolean IsNotSerialized
129                 {
130                         get
131                         {
132                                 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
133                         }
134                 }
135
136                 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
137
138                 [DebuggerHidden]
139                 [DebuggerStepThrough]
140                 public void SetValue (object obj, object value)
141                 {
142                         SetValue (obj, value, 0, null, null);
143                 }
144
145                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
146                 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
147
148                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
149                 {
150                         if (handle.Value == IntPtr.Zero)
151                                 throw new ArgumentException ("The handle is invalid.");
152                         return internal_from_handle_type (handle.Value, IntPtr.Zero);
153                 }
154
155                 [ComVisible (false)]
156                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
157                 {
158                         if (handle.Value == IntPtr.Zero)
159                                 throw new ArgumentException ("The handle is invalid.");
160                         FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
161                         if (fi == null)
162                                 throw new ArgumentException ("The field handle and the type handle are incompatible.");
163                         return fi;
164                 }
165
166                 //
167                 // Note: making this abstract imposes an implementation requirement
168                 //       on any class that derives from it.  However, since it's also
169                 //       internal, that means only classes inside corlib can derive
170                 //       from FieldInfo.  See
171                 //
172                 //          errors/cs0534-4.cs errors/CS0534-4-lib.cs
173                 //
174                 //          class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
175                 //
176                 internal virtual int GetFieldOffset ()
177                 {
178                         throw new SystemException ("This method should not be called");
179                 }
180
181                 [CLSCompliant(false)]
182                 [MonoTODO("Not implemented")]
183                 public virtual object GetValueDirect (TypedReference obj)
184                 {
185                         throw new NotImplementedException ();
186                 }
187
188                 [CLSCompliant(false)]
189                 [MonoTODO("Not implemented")]
190                 public virtual void SetValueDirect (TypedReference obj, object value)
191                 {
192                         throw new NotImplementedException ();
193                 }
194
195                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
196                 private extern MarshalAsAttribute get_marshal_info ();
197
198                 internal object[] GetPseudoCustomAttributes ()
199                 {
200                         int count = 0;
201
202                         if (IsNotSerialized)
203                                 count ++;
204
205                         if (DeclaringType.IsExplicitLayout)
206                                 count ++;
207
208                         MarshalAsAttribute marshalAs = get_marshal_info ();
209                         if (marshalAs != null)
210                                 count ++;
211
212                         if (count == 0)
213                                 return null;
214                         object[] attrs = new object [count];
215                         count = 0;
216
217                         if (IsNotSerialized)
218                                 attrs [count ++] = new NonSerializedAttribute ();
219                         if (DeclaringType.IsExplicitLayout)
220                                 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
221                         if (marshalAs != null)
222                                 attrs [count ++] = marshalAs;
223
224                         return attrs;
225                 }
226
227                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
228                 extern Type[] GetTypeModifiers (bool optional);
229
230                 public virtual Type[] GetOptionalCustomModifiers () {
231                         Type[] types = GetTypeModifiers (true);
232                         if (types == null)
233                                 return Type.EmptyTypes;
234                         return types;
235                 }
236
237                 public virtual Type[] GetRequiredCustomModifiers () {
238                         Type[] types = GetTypeModifiers (false);
239                         if (types == null)
240                                 return Type.EmptyTypes;
241                         return types;
242                 }
243
244                 public virtual object GetRawConstantValue ()
245                 {
246                         throw new NotSupportedException ("This non-CLS method is not implemented.");
247                 }
248
249
250                 public override bool Equals (object obj)
251                 {
252                         return obj == (object) this;
253                 }
254
255                 public override int GetHashCode ()
256                 {
257                         return base.GetHashCode ();
258                 }
259
260                 public static bool operator == (FieldInfo left, FieldInfo right)
261                 {
262                         if ((object)left == (object)right)
263                                 return true;
264                         if ((object)left == null ^ (object)right == null)
265                                 return false;
266                         return left.Equals (right);
267                 }
268
269                 public static bool operator != (FieldInfo left, FieldInfo right)
270                 {
271                         if ((object)left == (object)right)
272                                 return false;
273                         if ((object)left == null ^ (object)right == null)
274                                 return true;
275                         return !left.Equals (right);
276                 }
277                 
278                 public virtual bool IsSecurityCritical {
279                         get {
280                                 throw new NotImplementedException ();
281                         }
282                 }
283                 
284                 public virtual bool IsSecuritySafeCritical {
285                         get {
286                                 throw new NotImplementedException ();
287                         }
288                 }
289
290                 public virtual bool IsSecurityTransparent {
291                         get {
292                                 throw new NotImplementedException ();
293                         }
294                 }
295
296 #if !MOBILE
297                 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
298                 {
299                         throw new NotImplementedException ();
300                 }
301
302                 Type _FieldInfo.GetType ()
303                 {
304                         // Required or object::GetType becomes virtual final
305                         return base.GetType ();
306                 }
307
308                 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
309                 {
310                         throw new NotImplementedException ();
311                 }
312
313                 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
314                 {
315                         throw new NotImplementedException ();
316                 }
317
318                 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
319                 {
320                         throw new NotImplementedException ();
321                 }
322 #endif
323         }
324 }