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 {
39 [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
42 [ClassInterface(ClassInterfaceType.None)]
43 public abstract class FieldInfo : MemberInfo, _FieldInfo {
45 public abstract FieldAttributes Attributes {get;}
46 public abstract RuntimeFieldHandle FieldHandle {get;}
48 protected FieldInfo () {}
50 public abstract Type FieldType { get; }
52 public abstract object GetValue(object obj);
54 public override MemberTypes MemberType {
55 get { return MemberTypes.Field;}
60 get {return (Attributes & FieldAttributes.Literal) != 0;}
65 get {return (Attributes & FieldAttributes.Static) != 0;}
68 public bool IsInitOnly
70 get {return (Attributes & FieldAttributes.InitOnly) != 0;}
72 public Boolean IsPublic
76 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
79 public Boolean IsPrivate
83 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
86 public Boolean IsFamily
90 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
93 public Boolean IsAssembly
97 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
100 public Boolean IsFamilyAndAssembly
103 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
106 public Boolean IsFamilyOrAssembly
110 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
113 public Boolean IsPinvokeImpl
117 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
120 public Boolean IsSpecialName
124 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
127 public Boolean IsNotSerialized
131 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
135 public abstract void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
138 public new Type GetType ()
140 return base.GetType ();
145 [DebuggerStepThrough]
146 public void SetValue (object obj, object value)
148 SetValue (obj, value, 0, null, null);
151 [MethodImplAttribute(MethodImplOptions.InternalCall)]
152 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
154 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
156 return internal_from_handle_type (handle.Value, IntPtr.Zero);
161 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
163 return internal_from_handle_type (handle.Value, declaringType.Value);
168 // Note: making this abstract imposes an implementation requirement
169 // on any class that derives from it. However, since it's also
170 // internal, that means only classes inside corlib can derive
171 // from FieldInfo. See
173 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
175 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
177 internal virtual int GetFieldOffset ()
179 throw new SystemException ("This method should not be called");
182 [CLSCompliant(false)]
183 [MonoTODO("Not implemented")]
184 public virtual object GetValueDirect (TypedReference obj)
186 throw new NotImplementedException ();
189 [CLSCompliant(false)]
190 [MonoTODO("Not implemented")]
191 public virtual void SetValueDirect (TypedReference obj, object value)
193 throw new NotImplementedException ();
196 [MethodImplAttribute(MethodImplOptions.InternalCall)]
197 private extern UnmanagedMarshal GetUnmanagedMarshal ();
199 internal virtual UnmanagedMarshal UMarshal {
201 return GetUnmanagedMarshal ();
205 internal object[] GetPseudoCustomAttributes ()
212 if (DeclaringType.IsExplicitLayout)
215 UnmanagedMarshal marshalAs = UMarshal;
216 if (marshalAs != null)
221 object[] attrs = new object [count];
225 attrs [count ++] = new NonSerializedAttribute ();
226 if (DeclaringType.IsExplicitLayout)
227 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
228 if (marshalAs != null)
229 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
234 #if NET_2_0 || BOOTSTRAP_NET_2_0
235 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236 extern Type[] GetTypeModifiers (bool optional);
238 public virtual Type[] GetOptionalCustomModifiers () {
239 Type[] types = GetTypeModifiers (true);
241 return Type.EmptyTypes;
245 public virtual Type[] GetRequiredCustomModifiers () {
246 Type[] types = GetTypeModifiers (false);
248 return Type.EmptyTypes;
252 public virtual object GetRawConstantValue ()
254 throw new NotSupportedException ("This non-CLS method is not implemented.");
258 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
260 throw new NotImplementedException ();
263 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
265 throw new NotImplementedException ();
268 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
270 throw new NotImplementedException ();
273 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
275 throw new NotImplementedException ();