Adjust sleeping values
[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 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 using System.Diagnostics;
30 using System.Reflection.Emit;
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         public abstract class FieldInfo : MemberInfo, _FieldInfo {
42
43                 public abstract FieldAttributes Attributes {get;}
44                 public abstract RuntimeFieldHandle FieldHandle {get;}
45
46                 protected FieldInfo () {}
47                 
48                 public abstract Type FieldType { get; }
49
50                 public abstract object GetValue(object obj);
51
52                 public override MemberTypes MemberType {
53                         get { return MemberTypes.Field;}
54                 }
55
56                 public bool IsLiteral
57                 {
58                         get {return (Attributes & FieldAttributes.Literal) != 0;}
59                 } 
60
61                 public bool IsStatic
62                 {
63                         get {return (Attributes & FieldAttributes.Static) != 0;}
64                 } 
65
66                 public bool IsInitOnly
67                 {
68                         get {return (Attributes & FieldAttributes.InitOnly) != 0;}
69                 } 
70                 public Boolean IsPublic
71                 { 
72                         get
73                         {
74                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
75                         }
76                 }
77                 public Boolean IsPrivate
78                 {
79                         get
80                         {
81                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
82                         }
83                 }
84                 public Boolean IsFamily
85                 {
86                         get
87                         {
88                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
89                         }
90                 }
91                 public Boolean IsAssembly
92                 {
93                         get
94                         {
95                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
96                         }
97                 }
98                 public Boolean IsFamilyAndAssembly
99                 {
100                         get {
101                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
102                         }
103                 }
104                 public Boolean IsFamilyOrAssembly
105                 {
106                         get
107                         {
108                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
109                         }
110                 }
111                 public Boolean IsPinvokeImpl
112                 {
113                         get
114                         {
115                                 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
116                         }
117                 }
118                 public Boolean IsSpecialName
119                 {
120                         get
121                         {
122                                 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
123                         }
124                 }
125                 public Boolean IsNotSerialized
126                 {
127                         get
128                         {
129                                 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
130                         }
131                 }
132
133                 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
134
135                 [DebuggerHidden]
136                 [DebuggerStepThrough]
137                 public void SetValue (object obj, object value)
138                 {
139                         SetValue (obj, value, 0, null, null);
140                 }
141
142                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
143                 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
144
145                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
146                 {
147                         if (handle.Value == IntPtr.Zero)
148                                 throw new ArgumentException ("The handle is invalid.");
149                         return internal_from_handle_type (handle.Value, IntPtr.Zero);
150                 }
151
152                 [ComVisible (false)]
153                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
154                 {
155                         if (handle.Value == IntPtr.Zero)
156                                 throw new ArgumentException ("The handle is invalid.");
157                         FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
158                         if (fi == null)
159                                 throw new ArgumentException ("The field handle and the type handle are incompatible.");
160                         return fi;
161                 }
162
163                 //
164                 // Note: making this abstract imposes an implementation requirement
165                 //       on any class that derives from it.  However, since it's also
166                 //       internal, that means only classes inside corlib can derive
167                 //       from FieldInfo.  See
168                 //
169                 //          errors/cs0534-4.cs errors/CS0534-4-lib.cs
170                 //
171                 //          class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
172                 //
173                 internal virtual int GetFieldOffset ()
174                 {
175                         throw new SystemException ("This method should not be called");
176                 }
177
178                 [CLSCompliant(false)]
179                 [MonoTODO("Not implemented")]
180                 public virtual object GetValueDirect (TypedReference obj)
181                 {
182                         throw new NotImplementedException ();
183                 }
184
185                 [CLSCompliant(false)]
186                 [MonoTODO("Not implemented")]
187                 public virtual void SetValueDirect (TypedReference obj, object value)
188                 {
189                         throw new NotImplementedException ();
190                 }
191
192                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
193                 private extern UnmanagedMarshal GetUnmanagedMarshal ();
194
195                 internal virtual UnmanagedMarshal UMarshal {
196                         get {
197                                 return GetUnmanagedMarshal ();
198                         }
199                 }
200
201                 internal object[] GetPseudoCustomAttributes ()
202                 {
203                         int count = 0;
204
205                         if (IsNotSerialized)
206                                 count ++;
207
208                         if (DeclaringType.IsExplicitLayout)
209                                 count ++;
210
211                         UnmanagedMarshal marshalAs = UMarshal;
212                         if (marshalAs != null)
213                                 count ++;
214
215                         if (count == 0)
216                                 return null;
217                         object[] attrs = new object [count];
218                         count = 0;
219
220                         if (IsNotSerialized)
221                                 attrs [count ++] = new NonSerializedAttribute ();
222                         if (DeclaringType.IsExplicitLayout)
223                                 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
224                         if (marshalAs != null)
225                                 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
226
227                         return attrs;
228                 }
229
230                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
231                 extern Type[] GetTypeModifiers (bool optional);
232
233                 public virtual Type[] GetOptionalCustomModifiers () {
234                         Type[] types = GetTypeModifiers (true);
235                         if (types == null)
236                                 return Type.EmptyTypes;
237                         return types;
238                 }
239
240                 public virtual Type[] GetRequiredCustomModifiers () {
241                         Type[] types = GetTypeModifiers (false);
242                         if (types == null)
243                                 return Type.EmptyTypes;
244                         return types;
245                 }
246
247                 public virtual object GetRawConstantValue ()
248                 {
249                         throw new NotSupportedException ("This non-CLS method is not implemented.");
250                 }
251
252
253 #if NET_4_0
254                 public override bool Equals (object obj)
255                 {
256                         return obj == (object) this;
257                 }
258
259                 public override int GetHashCode ()
260                 {
261                         return base.GetHashCode ();
262                 }
263
264                 public static bool operator == (FieldInfo left, FieldInfo right)
265                 {
266                         if ((object)left == (object)right)
267                                 return true;
268                         if ((object)left == null ^ (object)right == null)
269                                 return false;
270                         return left.Equals (right);
271                 }
272
273                 public static bool operator != (FieldInfo left, FieldInfo right)
274                 {
275                         if ((object)left == (object)right)
276                                 return false;
277                         if ((object)left == null ^ (object)right == null)
278                                 return true;
279                         return !left.Equals (right);
280                 }
281 #endif
282                 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
283                 {
284                         throw new NotImplementedException ();
285                 }
286
287                 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
288                 {
289                         throw new NotImplementedException ();
290                 }
291
292                 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
293                 {
294                         throw new NotImplementedException ();
295                 }
296
297                 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
298                 {
299                         throw new NotImplementedException ();
300                 }
301         }
302 }