2 // System.Reflection.FieldInfo.cs
5 // Miguel de Icaza (miguel@ximian.com)
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).
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
30 using System.Diagnostics;
31 using System.Reflection.Emit;
32 using System.Globalization;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
36 namespace System.Reflection {
39 [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
41 [ClassInterface(ClassInterfaceType.None)]
42 public abstract class FieldInfo : MemberInfo, _FieldInfo {
44 public abstract FieldAttributes Attributes {get;}
45 public abstract RuntimeFieldHandle FieldHandle {get;}
47 protected FieldInfo () {}
49 public abstract Type FieldType { get; }
51 public abstract object GetValue(object obj);
53 public override MemberTypes MemberType {
54 get { return MemberTypes.Field;}
59 get {return (Attributes & FieldAttributes.Literal) != 0;}
64 get {return (Attributes & FieldAttributes.Static) != 0;}
67 public bool IsInitOnly
69 get {return (Attributes & FieldAttributes.InitOnly) != 0;}
71 public Boolean IsPublic
75 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
78 public Boolean IsPrivate
82 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
85 public Boolean IsFamily
89 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
92 public Boolean IsAssembly
96 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
99 public Boolean IsFamilyAndAssembly
102 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
105 public Boolean IsFamilyOrAssembly
109 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
112 public Boolean IsPinvokeImpl
116 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
119 public Boolean IsSpecialName
123 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
126 public Boolean IsNotSerialized
130 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
134 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
137 [DebuggerStepThrough]
138 public void SetValue (object obj, object value)
140 SetValue (obj, value, 0, null, null);
143 [MethodImplAttribute(MethodImplOptions.InternalCall)]
144 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
146 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
148 if (handle.Value == IntPtr.Zero)
149 throw new ArgumentException ("The handle is invalid.");
150 return internal_from_handle_type (handle.Value, IntPtr.Zero);
154 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
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);
160 throw new ArgumentException ("The field handle and the type handle are incompatible.");
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
170 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
172 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
174 internal virtual int GetFieldOffset ()
176 throw new SystemException ("This method should not be called");
179 [CLSCompliant(false)]
180 [MonoTODO("Not implemented")]
181 public virtual object GetValueDirect (TypedReference obj)
183 throw new NotImplementedException ();
186 [CLSCompliant(false)]
187 [MonoTODO("Not implemented")]
188 public virtual void SetValueDirect (TypedReference obj, object value)
190 throw new NotImplementedException ();
193 [MethodImplAttribute(MethodImplOptions.InternalCall)]
194 private extern UnmanagedMarshal GetUnmanagedMarshal ();
196 internal virtual UnmanagedMarshal UMarshal {
198 return GetUnmanagedMarshal ();
202 internal object[] GetPseudoCustomAttributes ()
209 if (DeclaringType.IsExplicitLayout)
212 UnmanagedMarshal marshalAs = UMarshal;
213 if (marshalAs != null)
218 object[] attrs = new object [count];
222 attrs [count ++] = new NonSerializedAttribute ();
223 if (DeclaringType.IsExplicitLayout)
224 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
225 if (marshalAs != null)
226 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
231 [MethodImplAttribute (MethodImplOptions.InternalCall)]
232 extern Type[] GetTypeModifiers (bool optional);
234 public virtual Type[] GetOptionalCustomModifiers () {
235 Type[] types = GetTypeModifiers (true);
237 return Type.EmptyTypes;
241 public virtual Type[] GetRequiredCustomModifiers () {
242 Type[] types = GetTypeModifiers (false);
244 return Type.EmptyTypes;
248 public virtual object GetRawConstantValue ()
250 throw new NotSupportedException ("This non-CLS method is not implemented.");
255 public override bool Equals (object obj)
257 return obj == (object) this;
260 public override int GetHashCode ()
262 return base.GetHashCode ();
265 public static bool operator == (FieldInfo left, FieldInfo right)
267 if ((object)left == (object)right)
269 if ((object)left == null ^ (object)right == null)
271 return left.Equals (right);
274 public static bool operator != (FieldInfo left, FieldInfo right)
276 if ((object)left == (object)right)
278 if ((object)left == null ^ (object)right == null)
280 return !left.Equals (right);
283 public virtual bool IsSecurityCritical {
285 throw new NotImplementedException ();
289 public virtual bool IsSecuritySafeCritical {
291 throw new NotImplementedException ();
295 public virtual bool IsSecurityTransparent {
297 throw new NotImplementedException ();
301 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
303 throw new NotImplementedException ();
306 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
308 throw new NotImplementedException ();
311 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
313 throw new NotImplementedException ();
316 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
318 throw new NotImplementedException ();