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)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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 {
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 UnmanagedMarshal GetUnmanagedMarshal ();
195 internal virtual UnmanagedMarshal UMarshal {
197 return GetUnmanagedMarshal ();
201 internal object[] GetPseudoCustomAttributes ()
208 if (DeclaringType.IsExplicitLayout)
211 UnmanagedMarshal marshalAs = UMarshal;
212 if (marshalAs != null)
217 object[] attrs = new object [count];
221 attrs [count ++] = new NonSerializedAttribute ();
222 if (DeclaringType.IsExplicitLayout)
223 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
224 if (marshalAs != null)
225 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
230 [MethodImplAttribute (MethodImplOptions.InternalCall)]
231 extern Type[] GetTypeModifiers (bool optional);
233 public virtual Type[] GetOptionalCustomModifiers () {
234 Type[] types = GetTypeModifiers (true);
236 return Type.EmptyTypes;
240 public virtual Type[] GetRequiredCustomModifiers () {
241 Type[] types = GetTypeModifiers (false);
243 return Type.EmptyTypes;
247 public virtual object GetRawConstantValue ()
249 throw new NotSupportedException ("This non-CLS method is not implemented.");
254 public override bool Equals (object obj)
256 return obj == (object) this;
259 public override int GetHashCode ()
261 return base.GetHashCode ();
264 public static bool operator == (FieldInfo left, FieldInfo right)
266 if ((object)left == (object)right)
268 if ((object)left == null ^ (object)right == null)
270 return left.Equals (right);
273 public static bool operator != (FieldInfo left, FieldInfo right)
275 if ((object)left == (object)right)
277 if ((object)left == null ^ (object)right == null)
279 return !left.Equals (right);
282 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
284 throw new NotImplementedException ();
287 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
289 throw new NotImplementedException ();
292 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
294 throw new NotImplementedException ();
297 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
299 throw new NotImplementedException ();