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)]
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;}
58 get {return (Attributes & FieldAttributes.Literal) != 0;}
63 get {return (Attributes & FieldAttributes.Static) != 0;}
66 public bool IsInitOnly
68 get {return (Attributes & FieldAttributes.InitOnly) != 0;}
70 public Boolean IsPublic
74 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
77 public Boolean IsPrivate
81 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
84 public Boolean IsFamily
88 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
91 public Boolean IsAssembly
95 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
98 public Boolean IsFamilyAndAssembly
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);
136 [DebuggerStepThrough]
137 public void SetValue (object obj, object value)
139 SetValue (obj, value, 0, null, null);
142 [MethodImplAttribute(MethodImplOptions.InternalCall)]
143 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
145 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
147 if (handle.Value == IntPtr.Zero)
148 throw new ArgumentException ("The handle is invalid.");
149 return internal_from_handle_type (handle.Value, IntPtr.Zero);
153 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
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);
159 throw new ArgumentException ("The field handle and the type handle are incompatible.");
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
169 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
171 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
173 internal virtual int GetFieldOffset ()
175 throw new SystemException ("This method should not be called");
178 [CLSCompliant(false)]
179 [MonoTODO("Not implemented")]
180 public virtual object GetValueDirect (TypedReference obj)
182 throw new NotImplementedException ();
185 [CLSCompliant(false)]
186 [MonoTODO("Not implemented")]
187 public virtual void SetValueDirect (TypedReference obj, object value)
189 throw new NotImplementedException ();
192 [MethodImplAttribute(MethodImplOptions.InternalCall)]
193 private extern MarshalAsAttribute get_marshal_info ();
195 internal object[] GetPseudoCustomAttributes ()
202 if (DeclaringType.IsExplicitLayout)
205 MarshalAsAttribute marshalAs = get_marshal_info ();
206 if (marshalAs != null)
211 object[] attrs = new object [count];
215 attrs [count ++] = new NonSerializedAttribute ();
216 if (DeclaringType.IsExplicitLayout)
217 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
218 if (marshalAs != null)
219 attrs [count ++] = marshalAs;
224 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225 extern Type[] GetTypeModifiers (bool optional);
227 public virtual Type[] GetOptionalCustomModifiers () {
228 Type[] types = GetTypeModifiers (true);
230 return Type.EmptyTypes;
234 public virtual Type[] GetRequiredCustomModifiers () {
235 Type[] types = GetTypeModifiers (false);
237 return Type.EmptyTypes;
241 public virtual object GetRawConstantValue ()
243 throw new NotSupportedException ("This non-CLS method is not implemented.");
248 public override bool Equals (object obj)
250 return obj == (object) this;
253 public override int GetHashCode ()
255 return base.GetHashCode ();
258 public static bool operator == (FieldInfo left, FieldInfo right)
260 if ((object)left == (object)right)
262 if ((object)left == null ^ (object)right == null)
264 return left.Equals (right);
267 public static bool operator != (FieldInfo left, FieldInfo right)
269 if ((object)left == (object)right)
271 if ((object)left == null ^ (object)right == null)
273 return !left.Equals (right);
276 public virtual bool IsSecurityCritical {
278 throw new NotImplementedException ();
282 public virtual bool IsSecuritySafeCritical {
284 throw new NotImplementedException ();
288 public virtual bool IsSecurityTransparent {
290 throw new NotImplementedException ();
294 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
296 throw new NotImplementedException ();
299 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
301 throw new NotImplementedException ();
304 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
306 throw new NotImplementedException ();
309 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
311 throw new NotImplementedException ();