3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // System.Reflection/MonoField.cs
27 // The class used to represent Fields from the mono runtime.
30 // Paolo Molaro (lupus@ximian.com)
32 // (C) 2001 Ximian, Inc. http://www.ximian.com
36 using System.Collections.Generic;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Runtime.Serialization;
41 using System.Diagnostics;
43 namespace System.Reflection {
45 abstract class RuntimeFieldInfo : FieldInfo
47 internal BindingFlags BindingFlags {
54 abstract class RtFieldInfo : RuntimeFieldInfo
56 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57 internal extern object UnsafeGetValue (object obj);
59 internal void CheckConsistency(Object target)
61 // only test instance fields
62 if ((Attributes & FieldAttributes.Static) != FieldAttributes.Static)
64 if (!DeclaringType.IsInstanceOfType(target))
68 #if FEATURE_LEGACYNETCF
69 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
70 throw new ArgumentNullException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
73 throw new TargetException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
77 throw new ArgumentException(
78 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_FieldDeclTarget"),
79 Name, DeclaringType, target.GetType()));
85 [DebuggerStepThroughAttribute]
86 [Diagnostics.DebuggerHidden]
87 internal void UnsafeSetValue (Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
89 bool domainInitialized = false;
90 RuntimeFieldHandle.SetValue (this, obj, value, null, Attributes, null, ref domainInitialized);
95 [StructLayout (LayoutKind.Sequential)]
96 internal class MonoField : RtFieldInfo, ISerializable {
97 internal IntPtr klass;
98 internal RuntimeFieldHandle fhandle;
101 FieldAttributes attrs;
103 public override FieldAttributes Attributes {
108 public override RuntimeFieldHandle FieldHandle {
114 [MethodImplAttribute(MethodImplOptions.InternalCall)]
115 extern Type ResolveType ();
117 public override Type FieldType {
120 type = ResolveType ();
125 [MethodImplAttribute(MethodImplOptions.InternalCall)]
126 private extern Type GetParentType (bool declaring);
128 public override Type ReflectedType {
130 return GetParentType (false);
133 public override Type DeclaringType {
135 return GetParentType (true);
138 public override string Name {
144 public override bool IsDefined (Type attributeType, bool inherit) {
145 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
148 public override object[] GetCustomAttributes( bool inherit) {
149 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
151 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
152 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
155 [MethodImplAttribute(MethodImplOptions.InternalCall)]
156 internal override extern int GetFieldOffset ();
158 [MethodImplAttribute(MethodImplOptions.InternalCall)]
159 private extern object GetValueInternal (object obj);
161 public override object GetValue (object obj)
165 throw new TargetException ("Non-static field requires a target");
166 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
167 throw new ArgumentException (string.Format (
168 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
169 Name, DeclaringType, obj.GetType ()),
175 return GetValueInternal (obj);
178 public override string ToString () {
179 return String.Format ("{0} {1}", FieldType, name);
182 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
185 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
189 throw new TargetException ("Non-static field requires a target");
190 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
191 throw new ArgumentException (string.Format (
192 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
193 Name, DeclaringType, obj.GetType ()),
197 throw new FieldAccessException ("Cannot set a constant field");
199 binder = Type.DefaultBinder;
202 RuntimeType fieldType = (RuntimeType) FieldType;
203 val = fieldType.CheckValue (val, binder, culture, invokeAttr);
205 SetValueInternal (this, obj, val);
208 internal MonoField Clone (string newName)
210 MonoField field = new MonoField ();
211 field.name = newName;
215 field.fhandle = fhandle;
220 public void GetObjectData (SerializationInfo info, StreamingContext context)
222 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
223 ToString(), MemberTypes.Field);
226 [MethodImplAttribute(MethodImplOptions.InternalCall)]
227 public override extern object GetRawConstantValue ();
229 public override IList<CustomAttributeData> GetCustomAttributesData () {
230 return CustomAttributeData.GetCustomAttributes (this);
233 void CheckGeneric () {
234 if (DeclaringType.ContainsGenericParameters)
235 throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");