2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Reflection / FieldInfo.cs
blob3243d1f97ad2029bec8678fd4d8f4c62887b1225
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.
29 using System.Diagnostics;
30 using System.Reflection.Emit;
31 using System.Globalization;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
35 namespace System.Reflection {
37 [ComVisible (true)]
38 [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
39 [Serializable]
40 [ClassInterface(ClassInterfaceType.None)]
41 public abstract class FieldInfo : MemberInfo, _FieldInfo {
43 public abstract FieldAttributes Attributes {get;}
44 public abstract RuntimeFieldHandle FieldHandle {get;}
46 protected FieldInfo () {}
48 public abstract Type FieldType { get; }
50 public abstract object GetValue(object obj);
52 public override MemberTypes MemberType {
53 get { return MemberTypes.Field;}
56 public bool IsLiteral
58 get {return (Attributes & FieldAttributes.Literal) != 0;}
61 public bool IsStatic
63 get {return (Attributes & FieldAttributes.Static) != 0;}
66 public bool IsInitOnly
68 get {return (Attributes & FieldAttributes.InitOnly) != 0;}
70 public Boolean IsPublic
72 get
74 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
77 public Boolean IsPrivate
79 get
81 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
84 public Boolean IsFamily
86 get
88 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
91 public Boolean IsAssembly
93 get
95 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
98 public Boolean IsFamilyAndAssembly
100 get {
101 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
104 public Boolean IsFamilyOrAssembly
108 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
111 public Boolean IsPinvokeImpl
115 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
118 public Boolean IsSpecialName
122 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
125 public Boolean IsNotSerialized
129 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
133 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
135 #if ONLY_1_1
136 public new Type GetType ()
138 return base.GetType ();
140 #endif
142 [DebuggerHidden]
143 [DebuggerStepThrough]
144 public void SetValue (object obj, object value)
146 SetValue (obj, value, 0, null, null);
149 [MethodImplAttribute(MethodImplOptions.InternalCall)]
150 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
152 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
154 if (handle.Value == IntPtr.Zero)
155 throw new ArgumentException ("The handle is invalid.");
156 return internal_from_handle_type (handle.Value, IntPtr.Zero);
159 [ComVisible (false)]
160 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
162 if (handle.Value == IntPtr.Zero)
163 throw new ArgumentException ("The handle is invalid.");
164 FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
165 if (fi == null)
166 throw new ArgumentException ("The field handle and the type handle are incompatible.");
167 return fi;
171 // Note: making this abstract imposes an implementation requirement
172 // on any class that derives from it. However, since it's also
173 // internal, that means only classes inside corlib can derive
174 // from FieldInfo. See
176 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
178 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
180 internal virtual int GetFieldOffset ()
182 throw new SystemException ("This method should not be called");
185 [CLSCompliant(false)]
186 [MonoTODO("Not implemented")]
187 public virtual object GetValueDirect (TypedReference obj)
189 throw new NotImplementedException ();
192 [CLSCompliant(false)]
193 [MonoTODO("Not implemented")]
194 public virtual void SetValueDirect (TypedReference obj, object value)
196 throw new NotImplementedException ();
199 [MethodImplAttribute(MethodImplOptions.InternalCall)]
200 private extern UnmanagedMarshal GetUnmanagedMarshal ();
202 internal virtual UnmanagedMarshal UMarshal {
203 get {
204 return GetUnmanagedMarshal ();
208 internal object[] GetPseudoCustomAttributes ()
210 int count = 0;
212 if (IsNotSerialized)
213 count ++;
215 if (DeclaringType.IsExplicitLayout)
216 count ++;
218 UnmanagedMarshal marshalAs = UMarshal;
219 if (marshalAs != null)
220 count ++;
222 if (count == 0)
223 return null;
224 object[] attrs = new object [count];
225 count = 0;
227 if (IsNotSerialized)
228 attrs [count ++] = new NonSerializedAttribute ();
229 if (DeclaringType.IsExplicitLayout)
230 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
231 if (marshalAs != null)
232 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
234 return attrs;
237 [MethodImplAttribute (MethodImplOptions.InternalCall)]
238 extern Type[] GetTypeModifiers (bool optional);
240 public virtual Type[] GetOptionalCustomModifiers () {
241 Type[] types = GetTypeModifiers (true);
242 if (types == null)
243 return Type.EmptyTypes;
244 return types;
247 public virtual Type[] GetRequiredCustomModifiers () {
248 Type[] types = GetTypeModifiers (false);
249 if (types == null)
250 return Type.EmptyTypes;
251 return types;
254 public virtual object GetRawConstantValue ()
256 throw new NotSupportedException ("This non-CLS method is not implemented.");
260 #if NET_4_0
261 public override bool Equals (object obj)
263 return obj == this;
266 public override int GetHashCode ()
268 return base.GetHashCode ();
271 public static bool operator == (FieldInfo left, FieldInfo right)
273 if ((object)left == (object)right)
274 return true;
275 if ((object)left == null ^ (object)right == null)
276 return false;
277 return left.Equals (right);
280 public static bool operator != (FieldInfo left, FieldInfo right)
282 if ((object)left == (object)right)
283 return false;
284 if ((object)left == null ^ (object)right == null)
285 return true;
286 return !left.Equals (right);
288 #endif
289 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
291 throw new NotImplementedException ();
294 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
296 throw new NotImplementedException ();
299 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
301 throw new NotImplementedException ();
304 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
306 throw new NotImplementedException ();