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.
30 // TODO: Mucho left to implement.
33 using System.Diagnostics;
34 using System.Reflection.Emit;
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
39 namespace System.Reflection {
43 [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
46 [ClassInterface(ClassInterfaceType.None)]
47 public abstract class FieldInfo : MemberInfo, _FieldInfo {
49 public abstract FieldAttributes Attributes {get;}
50 public abstract RuntimeFieldHandle FieldHandle {get;}
52 protected FieldInfo () {}
54 public abstract Type FieldType { get; }
56 public abstract object GetValue(object obj);
58 public override MemberTypes MemberType {
59 get { return MemberTypes.Field;}
64 get {return (Attributes & FieldAttributes.Literal) != 0;}
69 get {return (Attributes & FieldAttributes.Static) != 0;}
72 public bool IsInitOnly
74 get {return (Attributes & FieldAttributes.InitOnly) != 0;}
76 public Boolean IsPublic
80 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
83 public Boolean IsPrivate
87 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
90 public Boolean IsFamily
94 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
97 public Boolean IsAssembly
101 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
104 public Boolean IsFamilyAndAssembly
107 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
110 public Boolean IsFamilyOrAssembly
114 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
117 public Boolean IsPinvokeImpl
121 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
124 public Boolean IsSpecialName
128 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
131 public Boolean IsNotSerialized
135 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
139 public abstract void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
142 [DebuggerStepThrough]
143 public void SetValue (object obj, object value)
145 SetValue (obj, value, 0, null, null);
148 [MethodImplAttribute(MethodImplOptions.InternalCall)]
149 private static extern FieldInfo internal_from_handle (IntPtr handle);
151 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
153 return internal_from_handle (handle.Value);
157 // Note: making this abstract imposes an implementation requirement
158 // on any class that derives from it. However, since it's also
159 // internal, that means only classes inside corlib can derive
160 // from FieldInfo. See
162 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
164 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
166 internal virtual int GetFieldOffset ()
168 throw new SystemException ("This method should not be called");
171 [CLSCompliant(false)]
173 public virtual object GetValueDirect (TypedReference obj)
175 throw new NotImplementedException ();
178 [CLSCompliant(false)]
180 public virtual void SetValueDirect (TypedReference obj, object value)
182 throw new NotImplementedException ();
185 [MethodImplAttribute(MethodImplOptions.InternalCall)]
186 private extern UnmanagedMarshal GetUnmanagedMarshal ();
188 internal object[] GetPseudoCustomAttributes ()
195 if (DeclaringType.IsExplicitLayout)
198 UnmanagedMarshal marshalAs = GetUnmanagedMarshal ();
199 if (marshalAs != null)
204 object[] attrs = new object [count];
208 attrs [count ++] = new NonSerializedAttribute ();
209 if (DeclaringType.IsExplicitLayout)
210 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
211 if (marshalAs != null)
212 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
217 #if NET_2_0 || BOOTSTRAP_NET_2_0
218 [Obsolete ("Use FieldInfo.GetOptionalCustomModifiers().")]
219 public virtual Type[] OptionalCustomModifiers {
221 return GetOptionalCustomModifiers ();
225 [Obsolete ("Use FieldInfo.GetRequiredCustomModifiers().")]
226 public virtual Type[] RequiredCustomModifiers {
228 return GetRequiredCustomModifiers ();
233 public virtual Type[] GetOptionalCustomModifiers () {
234 throw new NotImplementedException ();
238 public virtual Type[] GetRequiredCustomModifiers () {
239 throw new NotImplementedException ();
243 #if NET_2_0 || BOOTSTRAP_NET_2_0
244 public abstract FieldInfo Mono_GetGenericFieldDefinition ();