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 public override Module Module
{
56 return GetRuntimeModule ();
60 internal RuntimeType
GetDeclaringTypeInternal ()
62 return (RuntimeType
) DeclaringType
;
65 RuntimeType ReflectedTypeInternal
{
67 return (RuntimeType
) ReflectedType
;
71 internal RuntimeModule
GetRuntimeModule ()
73 return GetDeclaringTypeInternal ().GetRuntimeModule ();
76 #region ISerializable Implementation
77 public void GetObjectData(SerializationInfo info
, StreamingContext context
)
80 throw new ArgumentNullException("info");
81 Contract
.EndContractBlock();
82 MemberInfoSerializationHolder
.GetSerializationInfo(
85 ReflectedTypeInternal
,
92 abstract class RtFieldInfo
: RuntimeFieldInfo
94 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
95 internal extern object UnsafeGetValue (object obj
);
97 internal void CheckConsistency(Object target
)
99 // only test instance fields
100 if ((Attributes
& FieldAttributes
.Static
) != FieldAttributes
.Static
)
102 if (!DeclaringType
.IsInstanceOfType(target
))
106 #if FEATURE_LEGACYNETCF
107 if (CompatibilitySwitches
.IsAppEarlierThanWindowsPhone8
)
108 throw new ArgumentNullException(Environment
.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
111 throw new TargetException(Environment
.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
115 throw new ArgumentException(
116 String
.Format(CultureInfo
.CurrentUICulture
, Environment
.GetResourceString("Arg_FieldDeclTarget"),
117 Name
, DeclaringType
, target
.GetType()));
123 [DebuggerStepThroughAttribute
]
124 [Diagnostics
.DebuggerHidden
]
125 internal void UnsafeSetValue (Object obj
, Object
value, BindingFlags invokeAttr
, Binder binder
, CultureInfo culture
)
127 bool domainInitialized
= false;
128 RuntimeFieldHandle
.SetValue (this, obj
, value, null, Attributes
, null, ref domainInitialized
);
131 [DebuggerStepThroughAttribute
]
132 [Diagnostics
.DebuggerHidden
]
133 public override void SetValueDirect(TypedReference obj
, Object
value)
136 throw new ArgumentException(Environment
.GetResourceString("Arg_TypedReference_Null"));
137 Contract
.EndContractBlock();
141 // Passing TypedReference by reference is easier to make correct in native code
142 RuntimeFieldHandle
.SetValueDirect(this, (RuntimeType
)FieldType
, &obj
, value, (RuntimeType
)DeclaringType
);
146 [DebuggerStepThroughAttribute
]
147 [Diagnostics
.DebuggerHidden
]
148 public override Object
GetValueDirect(TypedReference obj
)
151 throw new ArgumentException(Environment
.GetResourceString("Arg_TypedReference_Null"));
152 Contract
.EndContractBlock();
156 // Passing TypedReference by reference is easier to make correct in native code
157 return RuntimeFieldHandle
.GetValueDirect(this, (RuntimeType
)FieldType
, &obj
, (RuntimeType
)DeclaringType
);
163 [StructLayout (LayoutKind
.Sequential
)]
164 internal class MonoField
: RtFieldInfo
{
165 internal IntPtr klass
;
166 internal RuntimeFieldHandle fhandle
;
169 FieldAttributes attrs
;
171 public override FieldAttributes Attributes
{
176 public override RuntimeFieldHandle FieldHandle
{
182 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
183 extern Type
ResolveType ();
185 public override Type FieldType
{
188 type
= ResolveType ();
193 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
194 private extern Type
GetParentType (bool declaring
);
196 public override Type ReflectedType
{
198 return GetParentType (false);
201 public override Type DeclaringType
{
203 return GetParentType (true);
206 public override string Name
{
212 public override bool IsDefined (Type attributeType
, bool inherit
) {
213 return MonoCustomAttrs
.IsDefined (this, attributeType
, inherit
);
216 public override object[] GetCustomAttributes( bool inherit
) {
217 return MonoCustomAttrs
.GetCustomAttributes (this, inherit
);
219 public override object[] GetCustomAttributes( Type attributeType
, bool inherit
) {
220 return MonoCustomAttrs
.GetCustomAttributes (this, attributeType
, inherit
);
223 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
224 internal override extern int GetFieldOffset ();
226 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
227 private extern object GetValueInternal (object obj
);
229 public override object GetValue (object obj
)
233 throw new TargetException ("Non-static field requires a target");
234 if (!DeclaringType
.IsAssignableFrom (obj
.GetType ()))
235 throw new ArgumentException (string.Format (
236 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
237 Name
, DeclaringType
, obj
.GetType ()),
243 return GetValueInternal (obj
);
246 public override string ToString () {
247 return String
.Format ("{0} {1}", FieldType
, name
);
250 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
251 private static extern void SetValueInternal (FieldInfo fi
, object obj
, object value);
253 public override void SetValue (object obj
, object val
, BindingFlags invokeAttr
, Binder binder
, CultureInfo culture
)
257 throw new TargetException ("Non-static field requires a target");
258 if (!DeclaringType
.IsAssignableFrom (obj
.GetType ()))
259 throw new ArgumentException (string.Format (
260 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
261 Name
, DeclaringType
, obj
.GetType ()),
265 throw new FieldAccessException ("Cannot set a constant field");
267 binder
= Type
.DefaultBinder
;
270 RuntimeType fieldType
= (RuntimeType
) FieldType
;
271 val
= fieldType
.CheckValue (val
, binder
, culture
, invokeAttr
);
273 SetValueInternal (this, obj
, val
);
276 internal MonoField
Clone (string newName
)
278 MonoField field
= new MonoField ();
279 field
.name
= newName
;
283 field
.fhandle
= fhandle
;
287 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
288 public override extern object GetRawConstantValue ();
290 public override IList
<CustomAttributeData
> GetCustomAttributesData () {
291 return CustomAttributeData
.GetCustomAttributes (this);
294 void CheckGeneric () {
295 if (DeclaringType
.ContainsGenericParameters
)
296 throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
300 static int get_core_clr_security_level ()
305 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
306 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
307 public extern int get_core_clr_security_level ();
309 public override bool IsSecurityTransparent
{
310 get { return get_core_clr_security_level () == 0; }
313 public override bool IsSecurityCritical
{
314 get { return get_core_clr_security_level () > 0; }
317 public override bool IsSecuritySafeCritical
{
318 get { return get_core_clr_security_level () == 1; }
322 public sealed override bool HasSameMetadataDefinitionAs (MemberInfo other
) => HasSameMetadataDefinitionAsCore
<MonoField
> (other
);
324 public override int MetadataToken
{
326 return get_metadata_token (this);
330 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
331 internal static extern int get_metadata_token (MonoField monoField
);