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.Globalization;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
35 namespace System.Reflection {
38 [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
40 [ClassInterface(ClassInterfaceType.None)]
42 public abstract class FieldInfo : MemberInfo {
44 public abstract class FieldInfo : MemberInfo, _FieldInfo {
46 public abstract FieldAttributes Attributes {get;}
47 public abstract RuntimeFieldHandle FieldHandle {get;}
49 protected FieldInfo () {}
51 public abstract Type FieldType { get; }
53 public abstract object GetValue(object obj);
55 public override MemberTypes MemberType {
56 get { return MemberTypes.Field;}
61 get {return (Attributes & FieldAttributes.Literal) != 0;}
66 get {return (Attributes & FieldAttributes.Static) != 0;}
69 public bool IsInitOnly
71 get {return (Attributes & FieldAttributes.InitOnly) != 0;}
73 public Boolean IsPublic
77 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
80 public Boolean IsPrivate
84 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
87 public Boolean IsFamily
91 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
94 public Boolean IsAssembly
98 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
101 public Boolean IsFamilyAndAssembly
104 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
107 public Boolean IsFamilyOrAssembly
111 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
114 public Boolean IsPinvokeImpl
118 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
121 public Boolean IsSpecialName
125 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
128 public Boolean IsNotSerialized
132 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
136 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
139 [DebuggerStepThrough]
140 public void SetValue (object obj, object value)
142 SetValue (obj, value, 0, null, null);
145 [MethodImplAttribute(MethodImplOptions.InternalCall)]
146 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
148 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
150 if (handle.Value == IntPtr.Zero)
151 throw new ArgumentException ("The handle is invalid.");
152 return internal_from_handle_type (handle.Value, IntPtr.Zero);
156 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
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);
162 throw new ArgumentException ("The field handle and the type handle are incompatible.");
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
172 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
174 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
176 internal virtual int GetFieldOffset ()
178 throw new SystemException ("This method should not be called");
181 [CLSCompliant(false)]
182 [MonoTODO("Not implemented")]
183 public virtual object GetValueDirect (TypedReference obj)
185 throw new NotImplementedException ();
188 [CLSCompliant(false)]
189 [MonoTODO("Not implemented")]
190 public virtual void SetValueDirect (TypedReference obj, object value)
192 throw new NotImplementedException ();
195 [MethodImplAttribute(MethodImplOptions.InternalCall)]
196 private extern MarshalAsAttribute get_marshal_info ();
198 internal object[] GetPseudoCustomAttributes ()
205 if (DeclaringType.IsExplicitLayout)
208 MarshalAsAttribute marshalAs = get_marshal_info ();
209 if (marshalAs != null)
214 object[] attrs = new object [count];
218 attrs [count ++] = new NonSerializedAttribute ();
219 if (DeclaringType.IsExplicitLayout)
220 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
221 if (marshalAs != null)
222 attrs [count ++] = marshalAs;
227 [MethodImplAttribute (MethodImplOptions.InternalCall)]
228 extern Type[] GetTypeModifiers (bool optional);
230 public virtual Type[] GetOptionalCustomModifiers () {
231 Type[] types = GetTypeModifiers (true);
233 return Type.EmptyTypes;
237 public virtual Type[] GetRequiredCustomModifiers () {
238 Type[] types = GetTypeModifiers (false);
240 return Type.EmptyTypes;
244 public virtual object GetRawConstantValue ()
246 throw new NotSupportedException ("This non-CLS method is not implemented.");
250 public override bool Equals (object obj)
252 return obj == (object) this;
255 public override int GetHashCode ()
257 return base.GetHashCode ();
260 public static bool operator == (FieldInfo left, FieldInfo right)
262 if ((object)left == (object)right)
264 if ((object)left == null ^ (object)right == null)
266 return left.Equals (right);
269 public static bool operator != (FieldInfo left, FieldInfo right)
271 if ((object)left == (object)right)
273 if ((object)left == null ^ (object)right == null)
275 return !left.Equals (right);
278 public virtual bool IsSecurityCritical {
280 throw new NotImplementedException ();
284 public virtual bool IsSecuritySafeCritical {
286 throw new NotImplementedException ();
290 public virtual bool IsSecurityTransparent {
292 throw new NotImplementedException ();
297 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
299 throw new NotImplementedException ();
302 Type _FieldInfo.GetType ()
304 // Required or object::GetType becomes virtual final
305 return base.GetType ();
308 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
310 throw new NotImplementedException ();
313 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
315 throw new NotImplementedException ();
318 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
320 throw new NotImplementedException ();