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;
42 using System.Diagnostics.Contracts;
44 namespace System.Reflection {
46 abstract class RuntimeFieldInfo : FieldInfo, ISerializable
48 internal BindingFlags BindingFlags {
54 internal RuntimeType GetDeclaringTypeInternal ()
56 return (RuntimeType) DeclaringType;
59 RuntimeType ReflectedTypeInternal {
61 return (RuntimeType) ReflectedType;
65 #region ISerializable Implementation
66 public void GetObjectData(SerializationInfo info, StreamingContext context)
69 throw new ArgumentNullException("info");
70 Contract.EndContractBlock();
71 MemberInfoSerializationHolder.GetSerializationInfo(
74 ReflectedTypeInternal,
81 abstract class RtFieldInfo : RuntimeFieldInfo
83 [MethodImplAttribute(MethodImplOptions.InternalCall)]
84 internal extern object UnsafeGetValue (object obj);
86 internal void CheckConsistency(Object target)
88 // only test instance fields
89 if ((Attributes & FieldAttributes.Static) != FieldAttributes.Static)
91 if (!DeclaringType.IsInstanceOfType(target))
95 #if FEATURE_LEGACYNETCF
96 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
97 throw new ArgumentNullException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
100 throw new TargetException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
104 throw new ArgumentException(
105 String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_FieldDeclTarget"),
106 Name, DeclaringType, target.GetType()));
112 [DebuggerStepThroughAttribute]
113 [Diagnostics.DebuggerHidden]
114 internal void UnsafeSetValue (Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
116 bool domainInitialized = false;
117 RuntimeFieldHandle.SetValue (this, obj, value, null, Attributes, null, ref domainInitialized);
120 [DebuggerStepThroughAttribute]
121 [Diagnostics.DebuggerHidden]
122 public override void SetValueDirect(TypedReference obj, Object value)
125 throw new ArgumentException(Environment.GetResourceString("Arg_TypedReference_Null"));
126 Contract.EndContractBlock();
130 // Passing TypedReference by reference is easier to make correct in native code
131 RuntimeFieldHandle.SetValueDirect(this, (RuntimeType)FieldType, &obj, value, (RuntimeType)DeclaringType);
135 [DebuggerStepThroughAttribute]
136 [Diagnostics.DebuggerHidden]
137 public override Object GetValueDirect(TypedReference obj)
140 throw new ArgumentException(Environment.GetResourceString("Arg_TypedReference_Null"));
141 Contract.EndContractBlock();
145 // Passing TypedReference by reference is easier to make correct in native code
146 return RuntimeFieldHandle.GetValueDirect(this, (RuntimeType)FieldType, &obj, (RuntimeType)DeclaringType);
152 [StructLayout (LayoutKind.Sequential)]
153 internal class MonoField : RtFieldInfo {
154 internal IntPtr klass;
155 internal RuntimeFieldHandle fhandle;
158 FieldAttributes attrs;
160 public override FieldAttributes Attributes {
165 public override RuntimeFieldHandle FieldHandle {
171 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172 extern Type ResolveType ();
174 public override Type FieldType {
177 type = ResolveType ();
182 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183 private extern Type GetParentType (bool declaring);
185 public override Type ReflectedType {
187 return GetParentType (false);
190 public override Type DeclaringType {
192 return GetParentType (true);
195 public override string Name {
201 public override bool IsDefined (Type attributeType, bool inherit) {
202 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
205 public override object[] GetCustomAttributes( bool inherit) {
206 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
208 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
209 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
212 [MethodImplAttribute(MethodImplOptions.InternalCall)]
213 internal override extern int GetFieldOffset ();
215 [MethodImplAttribute(MethodImplOptions.InternalCall)]
216 private extern object GetValueInternal (object obj);
218 public override object GetValue (object obj)
222 throw new TargetException ("Non-static field requires a target");
223 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
224 throw new ArgumentException (string.Format (
225 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
226 Name, DeclaringType, obj.GetType ()),
232 return GetValueInternal (obj);
235 public override string ToString () {
236 return String.Format ("{0} {1}", FieldType, name);
239 [MethodImplAttribute(MethodImplOptions.InternalCall)]
240 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
242 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
246 throw new TargetException ("Non-static field requires a target");
247 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
248 throw new ArgumentException (string.Format (
249 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
250 Name, DeclaringType, obj.GetType ()),
254 throw new FieldAccessException ("Cannot set a constant field");
256 binder = Type.DefaultBinder;
259 RuntimeType fieldType = (RuntimeType) FieldType;
260 val = fieldType.CheckValue (val, binder, culture, invokeAttr);
262 SetValueInternal (this, obj, val);
265 internal MonoField Clone (string newName)
267 MonoField field = new MonoField ();
268 field.name = newName;
272 field.fhandle = fhandle;
276 [MethodImplAttribute(MethodImplOptions.InternalCall)]
277 public override extern object GetRawConstantValue ();
279 public override IList<CustomAttributeData> GetCustomAttributesData () {
280 return CustomAttributeData.GetCustomAttributes (this);
283 void CheckGeneric () {
284 if (DeclaringType.ContainsGenericParameters)
285 throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
288 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
289 [MethodImplAttribute(MethodImplOptions.InternalCall)]
290 public extern int get_core_clr_security_level ();
292 public override bool IsSecurityTransparent {
293 get { return get_core_clr_security_level () == 0; }
296 public override bool IsSecurityCritical {
297 get { return get_core_clr_security_level () > 0; }
300 public override bool IsSecuritySafeCritical {
301 get { return get_core_clr_security_level () == 1; }