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 (IntPtr handle);
154 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
156 return internal_from_handle (handle.Value);
160 // Note: making this abstract imposes an implementation requirement
161 // on any class that derives from it. However, since it's also
162 // internal, that means only classes inside corlib can derive
163 // from FieldInfo. See
165 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
167 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
169 internal virtual int GetFieldOffset ()
171 throw new SystemException ("This method should not be called");
174 [CLSCompliant(false)]
176 public virtual object GetValueDirect (TypedReference obj)
178 throw new NotImplementedException ();
181 [CLSCompliant(false)]
183 public virtual void SetValueDirect (TypedReference obj, object value)
185 throw new NotImplementedException ();
188 [MethodImplAttribute(MethodImplOptions.InternalCall)]
189 private extern UnmanagedMarshal GetUnmanagedMarshal ();
191 internal virtual UnmanagedMarshal UMarshal {
193 return GetUnmanagedMarshal ();
197 internal object[] GetPseudoCustomAttributes ()
204 if (DeclaringType.IsExplicitLayout)
207 UnmanagedMarshal marshalAs = UMarshal;
208 if (marshalAs != null)
213 object[] attrs = new object [count];
217 attrs [count ++] = new NonSerializedAttribute ();
218 if (DeclaringType.IsExplicitLayout)
219 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
220 if (marshalAs != null)
221 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
226 #if NET_2_0 || BOOTSTRAP_NET_2_0
228 public virtual Type[] GetOptionalCustomModifiers () {
229 throw new NotImplementedException ();
233 public virtual Type[] GetRequiredCustomModifiers () {
234 throw new NotImplementedException ();
237 public virtual object GetRawConstantValue ()
239 throw new NotSupportedException ("This non-CLS method is not implemented.");
243 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
245 throw new NotImplementedException ();
248 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
250 throw new NotImplementedException ();
253 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
255 throw new NotImplementedException ();
258 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
260 throw new NotImplementedException ();