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 public new Type
GetType ()
138 return base.GetType ();
143 [DebuggerStepThrough
]
144 public void SetValue (object obj
, object value)
146 SetValue (obj
, value, 0, null, null);
149 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
150 private static extern FieldInfo
internal_from_handle_type (IntPtr field_handle
, IntPtr type_handle
);
152 public static FieldInfo
GetFieldFromHandle (RuntimeFieldHandle handle
)
154 if (handle
.Value
== IntPtr
.Zero
)
155 throw new ArgumentException ("The handle is invalid.");
156 return internal_from_handle_type (handle
.Value
, IntPtr
.Zero
);
160 public static FieldInfo
GetFieldFromHandle (RuntimeFieldHandle handle
, RuntimeTypeHandle declaringType
)
162 if (handle
.Value
== IntPtr
.Zero
)
163 throw new ArgumentException ("The handle is invalid.");
164 FieldInfo fi
= internal_from_handle_type (handle
.Value
, declaringType
.Value
);
166 throw new ArgumentException ("The field handle and the type handle are incompatible.");
171 // Note: making this abstract imposes an implementation requirement
172 // on any class that derives from it. However, since it's also
173 // internal, that means only classes inside corlib can derive
174 // from FieldInfo. See
176 // errors/cs0534-4.cs errors/CS0534-4-lib.cs
178 // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
180 internal virtual int GetFieldOffset ()
182 throw new SystemException ("This method should not be called");
185 [CLSCompliant(false)]
186 [MonoTODO("Not implemented")]
187 public virtual object GetValueDirect (TypedReference obj
)
189 throw new NotImplementedException ();
192 [CLSCompliant(false)]
193 [MonoTODO("Not implemented")]
194 public virtual void SetValueDirect (TypedReference obj
, object value)
196 throw new NotImplementedException ();
199 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
200 private extern UnmanagedMarshal
GetUnmanagedMarshal ();
202 internal virtual UnmanagedMarshal UMarshal
{
204 return GetUnmanagedMarshal ();
208 internal object[] GetPseudoCustomAttributes ()
215 if (DeclaringType
.IsExplicitLayout
)
218 UnmanagedMarshal marshalAs
= UMarshal
;
219 if (marshalAs
!= null)
224 object[] attrs
= new object [count
];
228 attrs
[count
++] = new NonSerializedAttribute ();
229 if (DeclaringType
.IsExplicitLayout
)
230 attrs
[count
++] = new FieldOffsetAttribute (GetFieldOffset ());
231 if (marshalAs
!= null)
232 attrs
[count
++] = marshalAs
.ToMarshalAsAttribute ();
237 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
238 extern Type
[] GetTypeModifiers (bool optional
);
240 public virtual Type
[] GetOptionalCustomModifiers () {
241 Type
[] types
= GetTypeModifiers (true);
243 return Type
.EmptyTypes
;
247 public virtual Type
[] GetRequiredCustomModifiers () {
248 Type
[] types
= GetTypeModifiers (false);
250 return Type
.EmptyTypes
;
254 public virtual object GetRawConstantValue ()
256 throw new NotSupportedException ("This non-CLS method is not implemented.");
261 public override bool Equals (object obj
)
266 public override int GetHashCode ()
268 return base.GetHashCode ();
271 public static bool operator == (FieldInfo left
, FieldInfo right
)
273 if ((object)left
== (object)right
)
275 if ((object)left
== null ^
(object)right
== null)
277 return left
.Equals (right
);
280 public static bool operator != (FieldInfo left
, FieldInfo right
)
282 if ((object)left
== (object)right
)
284 if ((object)left
== null ^
(object)right
== null)
286 return !left
.Equals (right
);
289 void _FieldInfo
.GetIDsOfNames ([In
] ref Guid riid
, IntPtr rgszNames
, uint cNames
, uint lcid
, IntPtr rgDispId
)
291 throw new NotImplementedException ();
294 void _FieldInfo
.GetTypeInfo (uint iTInfo
, uint lcid
, IntPtr ppTInfo
)
296 throw new NotImplementedException ();
299 void _FieldInfo
.GetTypeInfoCount (out uint pcTInfo
)
301 throw new NotImplementedException ();
304 void _FieldInfo
.Invoke (uint dispIdMember
, [In
] ref Guid riid
, uint lcid
, short wFlags
, IntPtr pDispParams
, IntPtr pVarResult
, IntPtr pExcepInfo
, IntPtr puArgErr
)
306 throw new NotImplementedException ();