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